Hi, I want to make a script that your screen in telephone share in part and from one side is Player1 and on another Player2. When you put finger on the left half Player1 is moving to mid and when you click back he back to the start position. And the same with Player2. I wrote something like that but it’s not working how i want. Can someone help me?
using UnityEngine;
using System.Collections;
public class movingPlayer : MonoBehaviour {
public Camera camera;
void Update(){
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Screen.width/2))
{
transform.Translate(1,0,0);
}
else
transform.Translate(-1,0,0);
}
}
or i try to use this:
/*public Vector3 startPos;
void Start(){
startPos = this.transform.position;
}
void OnMouseDown(){
if(this.transform.position == startPos)
{
Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(screen.width/2,0, 0));
this.transform.position = worldPos;
}
else
{
this.transform.position = startPos;
}
}*/
but start position didn’t work beacuse when i click on my object he transform to start position where it was created.