-
Notifications
You must be signed in to change notification settings - Fork 0
/
player1.cs
44 lines (33 loc) · 1.17 KB
/
player1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class player1 : MonoBehaviour
{
public float playerboundar = 1f;
Rigidbody2D mybody1;
public float moveforce = 1f; //force to push the player1 (mybody1)
// Player (Character right)
void Start()
{
mybody1 = this.GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector3 pos = transform.position;
Vector3 moveplayer1 = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"),0,0) * moveforce * Time.deltaTime; // taking input from controller
pos += moveplayer1;
mybody1.AddForce(moveplayer1); // moving the player
// Setting the border limits for movement
float screenRatio = (float) Screen.width / (float) Screen.height;
float widthOrtho = Camera.main.orthographicSize * screenRatio;
if(pos.x + playerboundar > widthOrtho)
{
pos.x = widthOrtho - playerboundar;
}
if (pos.x - playerboundar < - widthOrtho)
{
pos.x = - widthOrtho + playerboundar;
}
transform.position = pos;
}
}