Hi, I’m making a scrolling doodle-jump style game with a spaceship. I have added a rotation script to my ship to make the ship roll while turning left/right but I can’t get it to work correctly.
The ship moves upwards along the Y axis and left to right alonge the x axis, i dont want it to move along the z axis at all but it seems to arc as the ship rotates…Any ideas?
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour
{
public float Speed = 16.0f; //left/right movement speed
private Quaternion localRotation;
public float speed = 16.0f; // left/right movement speed
public float rollSpeed = 5.0f;
public float rollAmount = 10.0f;
void Update() {
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * Speed, 0, 0);
Vector3 dir = Vector3.zero; //Android controls
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * Speed);
// first update the current rotation angles with input from acceleration axis
localRotation.y += Input.acceleration.x * speed;
localRotation.x += Input.acceleration.y * speed;
rollAmount *= 0.9f;
if (Input.GetKey("left")) {
rollAmount += rollSpeed ;
} else if (Input.GetKey("right")) {
rollAmount -= rollSpeed ;
}
transform.Rotate(0, rollAmount*Time.deltaTime, 0);
rigidbody.freezeRotation = true;
}
// Use this for initialization
void Start (){
// copy the rotation of the object itself into a buffer
localRotation = transform.rotation;
}
}
Thanks!
There’s an error in these lines:
localRotation.y += Input.acceleration.x * speed;
localRotation.x += Input.acceleration.y * speed;
localRotation is a quaternion, and their x,y,z,w components have nothing to do with the familiar angles we see in the Rotation field in the Inspector - this field is actually transform.eulerAngles. It’s a very common mistake, which most people do at first (me included).
In order to keep your logic, you should declare localRotation as a Vector3, and store transform.eulerAngles in it at Start - like below:
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour
{
public float Speed = 16.0f; //left/right movement speed
private Vector3 localRotation; // declare localRotation as a Vector3
public float speed = 16.0f; // left/right movement speed
public float rollSpeed = 5.0f;
public float rollAmount = 10.0f;
void Update() {
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * Speed, 0, 0);
Vector3 dir = Vector3.zero; //Android controls
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * Speed);
// first update the current rotation angles with input from acceleration axis
localRotation.y += Input.acceleration.x * speed;
localRotation.x += Input.acceleration.y * speed;
rollAmount *= 0.9f;
if (Input.GetKey("left")) {
rollAmount += rollSpeed ;
} else if (Input.GetKey("right")) {
rollAmount -= rollSpeed ;
}
transform.Rotate(0, rollAmount*Time.deltaTime, 0);
rigidbody.freezeRotation = true;
}
// Use this for initialization
void Start (){
// copy the rotation of the object itself into a buffer
localRotation = transform.eulerAngles;
}
}
But you’re modifying the Z position in the “Android controls” section, as @You (not you!) said - maybe you (not @You - God, this is becoming very confusing!) should use dir.y instead of dir.z in that section.