dzaiats
January 21, 2015, 12:50pm
1
Hello,
I cannot find solution. Please, help me.
I want to move my main game object (main character) by clicking on arrows (Up, Down, Left, Right) which is UI object on the screen. I was trying a lot of variant how to do it but everything was wrong. For example:
I created C# Script and attached it to the object “arrowUp”
void Update() {
if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) || (Input.GetMouseButton(0)))
{
player = GameObject.FindGameObjectWithTag(“Player”);
player.rigidbody2D.AddForce(newVector2(0,100));
Debug.Log(“Top”);
}
}
How to solve it? Please, help!
mgear
January 21, 2015, 12:57pm
2
Hm…thanks… I will try to look at this.
No, I didn’t find answer for my question. I still don’t understand how to do it.
Could You please post this thread?
I found answer. Need to use correct collider. I was using Circle collider, but need to use Sphere.
Finally C# code is:
RaycastHithit;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.tag == "arrow_bottom" )
Debug.Log( "Bottom");
}
}
I dont see how raycasting have much to do with the question but use Vector3.MoveTowards on objects.