- Home /
Move in direction camera is facing
I have a Google Cardboard object, with this script attached to the Head object and a Rigidbody componet:
public class PlayerMovement : MonoBehaviour {
public float thrust = 2.0f;
void FixedUpdate()
{
transform.Translate(Camera.main.transform.forward * thrust * Time.deltaTime );
}
}
I'd like to constantly be moving in the direction I am looking. But using this script when I change my gaze direction my movement doesn't always follow.
Sometimes it works perfectly, and other times the movement is completely off depending on the build of the game.
Seems like a simple script, but maybe I'm not doing something correctly???
why are you using FixedUpdate? you are not dealing with a rigidbody so you can just use update. Try using transform.Translate(Camera.main.transform.forward * thrust * Time.deltaTime, Space.Self);
Using Space.self does help in the editor. However whenever I build the game for Android I get very different movement then I do in the editor.
I am using FixedUpdate because I have a Rigidbody attached, like I stated above and I have a collider so when you are flying around you can collide with objects and not fly through them.
What can be the differance between in the editor and the build version for android?
Answer by FortisVenaliter · Aug 19, 2015 at 08:09 PM
transform.Translate works in local coordinates. Camera.transform.forward is in world coordinates.
Rather than using the translate function, try just using transform.position += ...
transform.position += this.transform.forward * thrust * Time.deltaTime;
This works great, using Camera.main doesn't work with Cardboard because I just noticed that it's the head object that has the rotation values, not the $$anonymous$$ain Camera.
how would we make this work if we want our camera to take directional inputs and change based off the direction we were looking?
Your answer
Follow this Question
Related Questions
How to smoothen movement of a child object? 1 Answer
Player walks to the right? 1 Answer
How to add Pinch to Zoom? 0 Answers
Camera Movement and angles 2 Answers
how to stop making script constantly want to look at the vector 2 Answers