- Home /
Question by
Bgjackso · Mar 25, 2017 at 08:52 AM ·
animationrotationquaternionlerpmelee attack
Quaternion rotation not behaving as intended
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);
}
}
}
Comment
If you want it to be following the player's rotation, surely you would use localRotation ins$$anonymous$$d of rotation?
Answer by RotemAV · Mar 28, 2017 at 07:28 AM
if u want to flip you're character ,u can scale it by -1, this will flip the character and all the children
public void Flip(float move)
{
if (move > 0 && !facingRight || move < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612113754im_/https://answers.unity.com/themes/thub/images/avi.jpg)