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”?