Have angle problem in rigidbody.moveRotation
Hi, i'm using rigidbody.moveRotation for rotating my plane character. and i have 2 problems.
It rotates well between 0 - 90, 270 - 0 degrees. but it doesn't rotate over that degrees.
in vertical rotation, it doesn't work as local rotation. it works like default global axis.
following is my code.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour
{
public float speed = 10;
public float rotationSpeed = 5;
public float turnSpeed = 20;
Vector3 dir;
Rigidbody rig = null;
float rotationX = 0;
float rotationY = 0;
float rotationZ = 0;
Vector3 vectorX = Vector3.zero;
Vector3 vectorY = Vector3.zero;
Vector3 vectorZ = Vector3.zero;
void Start ()
{
rig = transform.GetComponent<Rigidbody> ();
}
void Update ()
{
rotationY = Input.GetAxis ("Horizontal");
rotationX = Input.GetAxis ("Vertical");
rotationY *= Time.deltaTime * 50;
rotationX *= Time.deltaTime * 50;
vectorX =
new Vector3 (-rotationX, 0, 0) + new Vector3(transform.localEulerAngles.x, 0, 0);
vectorY =
new Vector3 (0, rotationY, 0) + new Vector3(0, transform.localEulerAngles.y, 0);
dir = vectorX + vectorY + vectorZ;
print ("Angles " + transform.localEulerAngles);
}
void FixedUpdate()
{
rig.MovePosition (transform.position + transform.forward * speed * Time.deltaTime);
rig.MoveRotation (Quaternion.Euler(dir));
}
}
I really need help about this problem.
void Update ()
{
// You can change this to global and try that as well
Vector3 currentEuler = transform.eulerAngles;
// Increment these x/y values by your inputs
currentEuler.x += Time.deltaTime * 50 * Input.GetAxis ("Horizontal");
currentEuler.y += Time.deltaTime * 50 * Input.GetAxis ("Vertical");
// Then just set this as our intended orientation for the next physics update
dir = currentEuler;
print ("Target Angles " + currentEuler);
}
Your code is a bit long for what you're trying to accomplish. See if this simplifies things.
As for local, it shouldn't matter for your problem. You are getting its current angles (local or not) and assigning increments based on your input. You can set the Z back to zero if you want, but then again you could just lock that rotation altogether.
edit: It is worth noting that setting euler angles like this can potentially lead to issues down the road. There is a reason Unity stores its angles with Quaternions by default, allowing us to convert into the more traditional Euler angle system when convenient.
Your answer
Follow this Question
Related Questions
copy rotation script doesent copy the full 360 degrees? 0 Answers
Why does this quaternion euler not compute? 0 Answers
Draw a ray from a gamobject and keep direction of the ray relative to the gameobjects rotation. 1 Answer
How to calculate and use new direction angle as a new forward direction. 1 Answer
How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer