- Home /
Sword rotation not working?
I wrote a script where I want my sword to essentially glide 90 degrees downwards and then glide back up to its original position. My script is not working and I'm not sure why. Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwordMovement : MonoBehaviour
{
private Vector3 newRotation;
private Vector3 originalRotation;
private bool rotating = false;
// Use this for initialization
void Start ()
{
newRotation = new Vector3(0, 0, -135);
originalRotation = new Vector3(0, 0, -45);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.R))
{
rotating = true;
}
if (rotating)
{
RotationChange ();
rotating = false;
}
}
void RotationChange()
{
transform.rotation = Quaternion.Euler(Vector3.Lerp (originalRotation, newRotation, Time.deltaTime));
transform.rotation = Quaternion.Euler(Vector3.Lerp (newRotation, originalRotation, Time.deltaTime));
Debug.Log ("The sword is moving");
}
}
All this does is cause my sword to rapidly snap to the bottom position and then do nothing whenever I press "R" again. I tested my two lines under the method RotationChange() individually; the first one does nothing and the second one causes the "snap" (which didn't make sense to me, since I thought the second one would bring the sword back up to its original position). Am I improperly using "quaternion" or "loop"?
Answer by OneCept-Games · Jan 02, 2018 at 10:15 PM
You should wait until your sword has reached newRotation, and then you can Lerp it back to originalRotation.
What do you mean by wait? I thought that the three arguments for "Vector3.Lerp" would move the sword from "originalRotation" to "newRotation" at the rate of "Time.deltaTime", i.e. waiting would already be accounted for; if waiting is not accounted for, what is the third argument (Time.deltaTime) actually doing? How do I go about identifying the exact moment that the sword reaches "newRotation"?
The 3rd argument is the time in seconds the rotation will use to go from arg1 to arg2, but you start a new Lerp right after, so the first Lerp will never finish. So start the first Lerp, wait for it to reach it's arg2 rotation, and then you can start the second one.
Your answer
![](https://koobas.hobune.stream/wayback/20220612145706im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Controlling the direction of a rotation 2 Answers
Issues with Lerps 2 Answers
Make Quaternion Slerp happen over a 1 second period 3 Answers
Quaternion.Lerp shaky while in coroutine? 1 Answer
how do i use Quaternion lerp on only the Y and Z axis 0 Answers