-
Notifications
You must be signed in to change notification settings - Fork 0
/
player2.cs
45 lines (34 loc) · 1.18 KB
/
player2.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
45
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class player2 : MonoBehaviour
{
public float playerboundar = 1;
Rigidbody2D mybody2;
public float moveforce = 1; //force to push the player2 (mybody1)
// Player (Character left)
void Start()
{
mybody2 = this.GetComponent<Rigidbody2D>();
}
//
void FixedUpdate()
{
Vector3 pos1 = transform.position;
Vector3 moveplayer2 = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal1"), 0, 0) * moveforce * Time.deltaTime; // taking input from controller
pos1 += moveplayer2;
mybody2.AddForce(moveplayer2); // moving the player
// Setting the border limits for movement
float screenRatio = (float)Screen.width / (float)Screen.height;
float widthOrtho = Camera.main.orthographicSize * screenRatio;
if (pos1.x + playerboundar > widthOrtho)
{
pos1.x = widthOrtho - playerboundar;
}
if (pos1.x - playerboundar < -widthOrtho)
{
pos1.x = -widthOrtho + playerboundar;
}
transform.position = pos1;
}
}