I am fairly new to Quaternions, and I seem to have run into an issue. The following script is attached to my player and the weapon GameObject is a child of the player. The motion is correct, however, the weapon object always points in 1 direction and does not follow the player’s direction. How can I fix this issue? From what I can tell, my issue is in my if/else of the motion. Should I not use Lerp and something else?
public class SwordMelee : MonoBehaviour {
public float rotationAngle = 90;
public float startingPosition = 0;
public float smooth = 5;
public GameObject weapon;
private bool swing = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
swing = false;
float animation = Input.GetAxis("Vertical") * rotationAngle;
float normal = Input.GetAxis("Vertical") * startingPosition;
Quaternion target = Quaternion.Euler(0, -animation, 0);
Quaternion atRest = Quaternion.Euler(0, normal, 0);
if (Input.GetButton("Fire1"))
{
swing = true;
}
if (swing == true)
{
weapon.SetActive(true);
weapon.transform.rotation = Quaternion.Lerp(weapon.transform.rotation, target, Time.deltaTime * smooth);
}
else
{
weapon.SetActive(false);
weapon.transform.rotation = Quaternion.Lerp(weapon.transform.rotation, atRest, Time.deltaTime * smooth);
}
}
}