- Home /
How do I use Joystick just to face/move forward?
On Windows version I can move the player using: player.velocity = transform.position += transform.forward Time.deltaTime speed;
By using the code below, the player will only face in the joystick's direction instead of facing forward like in a FPS game.
public float speed;
Rigidbody player;
void Start()
{
player = this.GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Set movement to joystick
Vector3 moveVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"),
0f,
CrossPlatformInputManager.GetAxis("Vertical")) * speed;
player.AddForce(moveVec);
//Set rotation to joystick
Vector3 lookVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal_2"),
0f,
CrossPlatformInputManager.GetAxis("Vertical_2"));
transform.LookAt(transform.position + lookVec);
in first person, the camera should be attached to the player. so despite your movement script, the camera is mounted behind the players head. its not done through script. you drag and drop the camera object onto the player object to make it a child object of the player.
The camera is attached to the player. The problem is the player movement is locked by horizontal and vertical.
Answer by Kishotta · May 06, 2017 at 05:53 PM
Delete the parts marked as "Set rotation to joystick".
If you don't want the transform to look at the look vector, remove the LookAt call.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Code Not Working 1 Answer
Two Horizontal Movement Axes Behaving Differently 0 Answers
Problem with inputs in command system 0 Answers