Hello, there is sphere and I want it to move in certain direction when it is (self) touched or clicked.Please suggest what should I do?
Here’s what I have tried.
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0) && transform.tag == "player1")
//Debug.Log("Pressed left click.");
{
transform.Translate (15f * Time.deltaTime,0f,0f);
}
}
May be I’m wrong but I think that you should use RayCast
It will be like this:
when you click on mouse you will create a ray on clicked possition. If the ray will hit a collider(in our case player collider) we will movee our player.
The code will be something like this:
Ray ray;
RaycastHit hit;
void Update()
{
if (Input.GetButtonDown("Fire1")){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
if(hit.collider.tag == "player1")
{
transform.Translate (15f * Time.deltaTime,0f,0f);
}
}
}
}
Hope it helps