- Home /
Rotation eulerangles not adjusting correctly...
I am trying to make a simple flappy bird clone (in order to act as a sort of practise / first game, NOTE: This is not my first game). I am trying to imitate the rotation that occurs whenever you "fly" (as in pointing up, then gradually point downwards). I have tried using smoothdamp and lerp but neither are working... The outcome of what the code is outputting, is that the bird just keeps rotation (z axis) with no stop.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Fly : MonoBehaviour
{
public float flyVelocity;
Rigidbody2D rb;
bool canFly;
Vector3 currentRotation;
public Vector3 targetRotation;
float rotationSpeed = 180f;
float smoothTime = 0.3f;
//Vector3 rotationVel = new Vector3 (0, flyVelocity, 0);
void Update()
{
currentRotation = transform.eulerAngles;
currentRotation.z -= 10f;
if(transform.eulerAngles.z <=-40)
{
currentRotation.z = -40;
}
transform.eulerAngles = currentRotation;
if(Input.GetButtonDown("Jump") && canFly)
{
currentRotation = new Vector3(
0,
0,
Mathf.LerpAngle(currentRotation.z, targetRotation.z, Time.deltaTime));
transform.eulerAngles = currentRotation;
rb.velocity = new Vector2(rb.velocity.x, flyVelocity);
}
}
}
Answer by toromano · Oct 21, 2015 at 08:50 AM
My suggestion would be something like this for rotation since rotation is related velocity in y diretion:
Quaternion upRotation;
Quaternion downRotation;
void Start () {
float upAngle = -40.0f; //modify this angle
float downAngle = 40.0f; //modify this angle
upRotation = Quaternion.EulerAngles( new Vector3(0,0,upAngle));
downRotation = Quaternion.EulerAngles( new Vector3(0,0,downAngle));
}
// Update is called once per frame
void Update()
{
if(rb.velocity.y < .0f) // when falling
{
transform.rotation = Quaternion.Lerp(transform.rotation,downRotation, Time.deltaTime * 5.0f);
}
else
{
transform.rotation = Quaternion.Lerp(transform.rotation,upRotation, Time.deltaTime * 5.0f);
}
}
$$anonymous$$ate, you're a bloody legend. Works perfectly... Exactly what I needed :)... Except for the fact that the character is upside-down. Just for future reference (if anyone were to see this page in the future), if you don't want the character to be upside-down, set the upAngle to -100, and the downAngle to 100. It worked for me.
NOTE: Rotations may vary, my rotations were 0,0,0.
Answer by PictonicStudio · Oct 20, 2015 at 05:17 PM
Try this method instead.
public float jumpforce = 100;
public Rigidbody2D mainRigidbody;
public bool grounded;
private float tempAngle;
private void Start()
{
mainRigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x,transform.position.y-0.42f), -Vector2.up);
//Change 0.46f to half of the 2d collide height.
grounded = hit.distance <= 0.46f ? true : false;
Debug.Log(hit.distance);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
mainRigidbody.AddForce(Vector3.up * jumpforce);
}
if (mainRigidbody.velocity.y < 0)
{
tempAngle = -hit.distance * (jumpforce / 45);
}
else
{
tempAngle = 0;
}
transform.eulerAngles = new Vector3(0,0,Mathf.LerpAngle(transform.eulerAngles.z,tempAngle,Time.deltaTime*6));
}
Sorry but this isn't working, it seems to be a replacement jump script... I am looking for a fix for my rotation script...
Your answer
Follow this Question
Related Questions
make object rotate towards its direction after bounce 1 Answer
Equivalent to this using torque 1 Answer
Rotating projectile 2 Answers