- Home /
Help My movement is broken!!
I'm making a FPS and i need help with the movement i already have a script for locking my mouse and making my player follow it but the movement it broken if i look one way it doesn't work like it looks that way but it doesn't move that way when I press W Plz help!!
Hi, you described your issue quite well, but we cannot help without seeing code that you implement for moving, can you attach your code so we could check if there is any issue in there?
Answer by BobMen645 · May 12, 2020 at 10:09 PM
Sure thing here you go using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Movement : MonoBehaviour { CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
if (characterController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
}
Answer by ADiSiN · May 13, 2020 at 01:21 AM
Thank you for showing your code.
Take a look at this one:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movement : MonoBehaviour
{
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
if (characterController.isGrounded)
{
float xDir = Input.GetAxis("Horizontal");
float zDir = Input.GetAxis("Vertical");
moveDirection = transform.right * xDir + transform.forward * zDir;
moveDirection.y = 0; // Otherwise we will fly towards camera direction if it points upwards
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
}
Why in your code you didn't move towards forward direction of your view is because you never used it in code.
In the code abow we calculate Horizontal and Vertical input separately and then calculate overall moveDirection by multiplying input values to our right and forward direction of transform what makes it take into account our transform orientation. However, we shouldn't forget to reset moveDirection.y to 0, otherwise if we look slightly upwards or downwards it will fly into that direction.
Hope that helps.
Thank you but there is one problem with the code can u help me So basically A is forward D is backwards W is left and S is right I'm stuck because the character controller is weird can you help I looked in the code and there is nothing there for the buttons so I'm stuck Thank you
The "Horizontal" and "Vertical" Axises represent buttons a,d,left,right arrows and w,s,up,down arrows accordingly.
You can find it in Edit-Project Settings-Input $$anonymous$$anager.
Depends on how you want your gameplay to be controlled you can swap these two lines:
float xDir = Input.GetAxis("Horizontal");
float zDir = Input.GetAxis("Vertical");
To that:
float xDir = Input.GetAxis("Vertical");
float zDir = Input.GetAxis("Horizontal");
To change which buttons will represent forward which side direction.
Here is official doc with more information: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
Your answer
Follow this Question
Related Questions
Camera gets stuck when cursor is locked 0 Answers
Camera movement and gun follow in unity 1 Answer
Camera follow a sphere which look at another object on the ground 0 Answers
Free camera look question 2 Answers