0
\$\begingroup\$

I am new to Unity and I am confused about how to solve this problem. Initially, I want to make a platform rotate to its center vector up like this.

transform.Rotate (Vector3.up, speed * Time.deltaTime);

Then, I want to stop it from rotating when the mouse select it, like:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray, out hit)){
  //find a way to stop rotate
}

I cannot figure out how to stop it from rotating. Can anyone help me, please?

\$\endgroup\$
1
\$\begingroup\$

Well, you can use OnMouseDown message to do so.

What you have to do is to make sure that your platform have any collider attached. Now implement OnMouseDown with the help of a flag. Like,

bool _shouldRotate = true;

void Update()
{
    if (_shouldRotate)
        transform.Rotate (Vector3.up, speed * Time.deltaTime);
}

void OnMouseDown()
{
    if (_shouldRotate)
        _shouldRotate = false;
}
\$\endgroup\$
0
\$\begingroup\$

Firstly add a tag to the platform object eg. "platform", then in the script where you are rotating the object make sure it checks if certain Boolean eg. ROT is true, then in the raycast from mouse script check the tag of the object like this: if(hit.collider.tag == "platform")

If it is then set the Boolean ROT to false which will prevent the line rotating the platform from running.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.