Should I raycast? Or is there another funtion I can use?
Hello friends I am new with Unity and wondering if there are alternatives to ray casting. My game is a road traffic manager and when the players taps a car I want it to stop. This is my code for the movement. I tried the getmousebuttondown function but realized that every gameobject with this script attached would just stop or go depending on when they spawn. Thank you in advance for your helpful advice :)
public float SPEED;
public Transform[] FirstPossition;
int StopPossition;
public bool GO = false;
void Para()
{
if (Input.GetMouseButtonDown (0))
{
GO = !GO;
if (GO)
{
SPEED = 5;
} else {
SPEED = 0;
}
//GO = false;
}
}
void Start () {
StopPossition = 0;
}
void Update()
{
if (transform.position == FirstPossition [StopPossition].transform.position)
{
StopPossition+=Random.Range(0,4);
}
transform.position = Vector3.MoveTowards (transform.position, FirstPossition [StopPossition].position, SPEED * Time.deltaTime);
if (StopPossition >= 5)
{
StopPossition = 0;
}
Para ();
Debug.Log (StopPossition);
}
Answer by HenryStrattonFW · Feb 12, 2017 at 02:19 PM
You could do raycasting yourself which gives you a lot more flexibility and control should you need it later on. However if your objects have colliders on them you can also use a built-in method of monobehaviours that responds to clicking on those colliders (sort of a built-in raycast for you).
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
@HenryStrattonFW did your advice and it works! I got a reference code from here http://answers.unity3d.com/questions/157166/clicking-to-change-material-click-again-to-change.html a bit old but it did job! :) Thanks again! I'll work on my raycasting next time!
Your answer
Follow this Question
Related Questions
Making GameObject move in the direction of my mouse (3d) 0 Answers
Moving around mouse 0 Answers
Why isn't my space ship rotating down with my mouse? 1 Answer
SmoothDamp not going to target position 0 Answers
Issue with getting the mouse position in 3D and moving a gameobject on the y & z axis 0 Answers