- Home /
Player moving/rotating along a single axis
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!
You don't want the ship to move on the z axis...but you have "dir.z = Input.acceleration.y"... That might be your problem. You can also use a Vector2 ins$$anonymous$$d of a Vector3 since you only want to deal with the x and y axes
You mean as your spaceship turns left on a 2D plane , you rotate it to face a certain direction , while doing so you want it to tilt to give a feeling of it turning?
Answer by aldonaletto · May 26, 2012 at 07:56 PM
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.
Thanks for the help so far, I have tried what you suggested but im not sure i explained myself correctly.
The ship moves upwards along the Y axis at all times(though the camera is static, following the ship at all times, the spaceship stays at bottom-middle of screen), gaining speedboosts from pickups, you tilt the phone/use L/R arrows to move left and right along the X axis in 2-D style. I just need the ship to bank to the left/right whilst still pointing in the same direction (up on Y axis). The ship should never move along the Z axis as the pickups spawn at 0,0,0 then upwards along the Y axis, if the player drifts into the Z axis you can't get the pickups.
Thanks again