- Home /
Question by
PadvaGames · Sep 07, 2020 at 04:47 PM ·
scripting problem
FPS Movement help
I scripted the following scripts:
using UnityEngine;
public class Mouse : MonoBehaviour
{
// horizontal rotation speed
public float horizontalSpeed = 1f;
// vertical rotation speed
public float verticalSpeed = 1f;
private float xRotation = 0.0f;
private float yRotation = 0.0f;
private Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
cam.transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
}
}
using UnityEngine;
public class Move : MonoBehaviour
{
public float walkSpeed = 6.0F;
public float jumpSpeed = 2.0F;
public float runSpeed = 8.0F;
public float gravity = 9.8F;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
But, when I run the game, the movements are not working correctly: the player not moves forward when pressing W, but it moves alwais in the same direction.
Comment
AFAIK you don't need to call moveDirection = transform.TransformDirection(moveDirection);
. The CharacterController
already handles this for you.
Thanks Hellium! That made for some reason the movements smoother, but didn't fixed the problem.