- Home /
How to make an object roate 180 on Z axis on each mouse click
I'm working on a project for school and I need an object to rotate 180 degrees when selected with the mouse. The first attempt only got the object to rotate a little each time however with some assistance I came up with the code below. The issue however is that while it rotates the first time 180, every other time it rotates 360. How do I correct my script so that it is operating properly (rotating 180 each time it is selected - basically flipping to upside down) Below is the code I currently have that is giving me the issue.
Many thanks,
Chris
using UnityEngine; using System.Collections;
public class Select3 : MonoBehaviour { float speed = 0;
void Start ()
{
}
void Update ()
{
transform.Rotate(0, 0, Time.deltaTime*speed);
Debug.Log (gameObject.transform.rotation.z);
Debug.Log (gameObject.transform.rotation.z *180);
if (Mathf.Abs(gameObject.transform.rotation.z)*180 >= 179.995)
{
speed = 0;
}
}
void OnMouseDown()
{
speed = 100;
}
}
transform.rotation.z return a "Rotator" not a float, please use transform.rotation.eulerAngles.z
Answer by SmalleeStudio · Nov 10, 2014 at 06:52 AM
void Start () {
}
void Update () {
alpha += speed ;
alpha = Mathf.Clamp(alpha, 0, 1) ;
transform.rotation.eulerAngles.z = Mathf.Lerp(0, 180, alpha) ;
}
void OnMouseDown(){
speed*=-1 ;
}
"speed" variable must < 1 or you will see the instantly change, suggest 0.05
I'm sorry I tried it and I get a lot of errors. I tried as new code and also put it in to my existing code. The big errors that I get are that
"The name 'alpha does not exist in the current context"
Cannot modify a value type return value of "UnityEngine.Quaternion.eulerAngles". Consider storing the value in a temporary variable
Cannot modify a value type return value of "UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable
Those are the errors I get if it's a new script and if it's compiled with the original. I appreciate the help in suggesting a code, just not sure why I get the errors.
$$anonymous$$any Thanks
Chris
You need to define the variables alpha and speed as well as another variable of type Vector3
which you should save transform.rotation.eulerAngles
to, then modify, then set transform.rotation.eulerAngles
to. Perhaps do a few tutorials first if you don't understand variables :)
thank you Scribe. I have been doing tutorials. I'm just not picking it up that easily. However I just didn't understand what alpha meant. I'll go back over the tutorials.
Sorry about unclear code. And Thank for Scribe's comment.
alpha means the value's position "property" between two float.
If alpha = 0, the value will return the first value in $$anonymous$$athf.Lerp(first,second,alpha) . If alpha=1, return second value.
So if alpha =0.5, it may be 90, the average.
Your answer
Follow this Question
Related Questions
Limit rotation of z axis of a 2d object which rotates with mouse click and drag 1 Answer
Unity2D problem with negative z rotation shooting bullet towards mouse click 1 Answer
Rotating Produces Odd Results [Unity 2D] 1 Answer
Rotation around Z axis wont lock correctly (2D) 1 Answer
How to rotate a Character Controller on the X or Z axis ? (Not for movement) 0 Answers