I’m making a game with a first-person view, and I decided not to use the prefab first-person controller and try to create it all from scratch. I finally got the camera to move when I move the mouse, but the WASD controls don’t seem to be correct. I can go forward, backward, and sideways with WASD but in many games they change with the camera. In the game for example, when I first start up and go forward with the W/forward-arrow key, and I turn the camera in the opposite direction, I can’t use W to go into that direction. I have to use the S key to go back. This is the same with all directions. I just need to know what function I need so that the direction I’m facing with the camera, the controls become oriented with that direction. I’ve been looking, I’m not sure if there are any other up-to-date questions about this.
using UnityEngine;
using System.Collections;
public class WalkScript : MonoBehaviour {
private Transform player;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
float xAxisValue = Input.GetAxis("Horizontal");
float zAxisValue = Input.GetAxis("Vertical");
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}