- Home /
How do I rotate my player and still make it go where I tilt my phone?
Ok, I'm not sure if I asked that right.
Anyhow, I'm making this 2d perspective game, one similar to that really old game "asteriods" where the camera is on top of the player. I use the accelerometer to move my player up, down, left and right. then I have 2 GUI buttons for rotating my player left and right. the problem is, when i rotate the player, the directions of the accelerometer gets ruined (for example, if i rotate the player then tilt the phone to the left, instead of the player going to the left, it goes to some other direction, which is very frustrating).
Sorry for being too demanding. I tried searching for answers and failed, so this is pretty much my last resort.
Here's my code for the accelerometer:
var speed = 20.0;
function Update () {
var xMove : float = -Input.acceleration.y * Time.deltaTime * speed;
var yMove : float = Input.acceleration.x * Time.deltaTime * speed;
transform.Translate(Vector3(xMove,yMove,0));
transform.position.x = Mathf.Clamp(transform.position.x, -Screen.width*0.045, Screen.width*0.045);
transform.position.y = Mathf.Clamp(transform.position.y, -Screen.height*0.045, Screen.height*0.045);
}
Here's my code for turning:
function OnGUI () {
if(GUI.RepeatButton(Rect(Screen.width * .01, Screen.height * .80, 50, 50), "<"))
{
transform.Rotate(0,0, rotationSpeed * Time.deltaTime);
}
if(GUI.RepeatButton(Rect(Screen.width * .20, Screen.height * .80, 50, 50), ">"))
{
transform.Rotate(0,0, -rotationSpeed * Time.deltaTime);
}
Answer by RetepTrun · Jul 07, 2012 at 08:40 PM
Sounds like you want him to move by the world coordinates not his local coordinates.
Look at this
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html
// Move the object upward in world space 1 unit/second.
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
thanks man!! it's funny that Space.World was the only thing I needed. XD
Your answer
Follow this Question
Related Questions
how to rotate 2d obj on android 1 Answer
Rotate Camera using Accelerometer 0 Answers
Mapping accelerometer to Camera's position 2 Answers
How to create a timer with the new UI text? 1 Answer
android accelerometer rotation 0 Answers