Question by
zacharygough07 · Dec 05, 2021 at 08:42 AM ·
2drotationrigidbody2dslerp
Unity2D Choppy Slerp Update,,Rigidbody rotation choppy in update
I have been making a Flappy Bird clone, and I am having trouble with the rotation of the bird. It's very choppy with Update(). It is not choppy with FixedUpdate() in the editor, but is in the build. Here is my script (you can emulate it with a rigidbody sideways triangle):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdFly : MonoBehaviour
{
public GameManager gameManager;
public float velocity = 1;
private Rigidbody2D rb;
public bool dead = false;
public AudioSource wing;
public AudioSource hit;
public AudioSource die;
public Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//Jump
if (Input.GetMouseButtonDown(0) && !dead)
{
rb.velocity = Vector2.up * velocity;
wing.Play();
}
}
void FixedUpdate()
{
if (!dead)
{
//Rotate to Velocity
//Vector2 vel = rb.velocity;
//float ang = Mathf.Atan2(vel.y, 10) * Mathf.Rad2Deg;
//transform.rotation = Quaternion.Euler(new Vector3(0, 0, ang * 4));
var dir = rb.velocity;
var angle = Mathf.Atan2(dir.y, 1.5f) * Mathf.Rad2Deg;
var q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, 100);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (!dead)
{
dead = true;
rb.AddForce(Vector2.left / 2, ForceMode2D.Impulse);
hit.Play();
die.Play();
anim.speed = 0;
gameManager.GameOver();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Can not use properties of rigitbody2d 1 Answer
2D Sprite is floating when I move him (Rigidbody2D) 0 Answers
2d keep player upright 1 Answer
How to use Quaternion.Slerp with transform.LookAt? 3 Answers