- Home /
instant rotate an object
Hello everyone.
i'm trying to instant rotate my shark that is moving between two point with pingpong, i'm noob and i can't figure out how to do it, i already tried many things, transform.rotate(doesn't instantly rotate it), and also i tried with checkpoints, when hit the checkpoint1 collider2d it will rotate, but doesn't work, maybe it's a really stupid things but i can't figure out how to do it, anyone can help me? this is the code.
private Vector2 pos1 = new Vector2(21.00f,-4.04f);
private Vector2 pos2 = new Vector2(36.00f,-4.04f);
public float speed = 1.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = Vector2.Lerp(pos1, pos2, Mathf.PingPong(Time.time * speed,1.0f));
}
void OnTriggerEnter2D (Collider2D other) {
if(other.tag == "checkpoint1") {
transform.rotation = Quaternion.Euler(0,-180,0);
}
else if(other.tag == "checkpoint2") {
transform.rotation = Quaternion.Euler(0,180,0);
}
}
}
Answer by Eno-Khaon · Apr 09, 2018 at 04:29 AM
By your uses of transform.rotation in your script example, you're setting the same rotation in both cases.
Because Euler angles use a range from 0-360 degrees for a full rotation (and any other number can be converted into that range), the values you use of 180 and -180 both resolve to the same value of 180 degrees.
That said, there are numerous ways to implement this, so here's another example as food for thought:
Quaternion noRotation = Quaternion.AngleAxis(0.0f, Vector3.up);
Quaternion halfRotation = Quaternion.AngleAxis(180.0f, Vector3.up);
With this in mind, you could also potentially just reuse the halfRotation every time you want to change direction (with a very slight possibility of visible error in facing after many, many, MANY hours)
transform.rotation *= halfRotation;
Answer by saml_baker · Apr 09, 2018 at 03:40 AM
Try using Rigidbody.rotation. It will let you teleport between rotations using the rigidbody.
Answer by mirkojpn · Apr 09, 2018 at 04:28 AM
thank's you for your answer, i read that RigidBody2D.rotation is going to rotate the z axis, but what i need is to rotate de y axis.
Your answer
Follow this Question
Related Questions
How do I rotate a game object in 45 degree steps per keystroke? 2 Answers
Rotate object in the direction where it is going 1 Answer
How do I rotate a quaternion with euler angles? 0 Answers
Quaternion.Slerp problem... 1 Answer
Rotation with a slider 2 Answers