- Home /
Question by
fluxhackspro · Aug 06, 2019 at 03:55 PM ·
movementfirst person controller
Simple first person controller camera rotation
Hello Im creating a simple first person controller and nailed the movement thanks to unity script reference, but i cant seem to implement camera rotation and movement based on the cameras rotation. How does one do this? Im not searching for a "CameraMove" script, there has to be a simpler way that fits inside my movement script here. Any help is welcome ~Flux
public class Movement : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
Vector3 moveDirection = Vector3.zero;
CharacterController controller;
void Start ()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = Camera.main.transform.position + 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);
}
}
Comment