Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class PlayerController : MonoBehaviour {
- Rigidbody rb;
- public Camera mainCam;
- public float speed = 5f;
- public float sensitivity = 3f;
- void Start ()
- {
- rb = GetComponent<Rigidbody>();
- }
- void Update ()
- {
- //Movement
- float xMov = Input.GetAxisRaw("Horizontal");
- float zMov = Input.GetAxisRaw("Vertical");
- Vector3 movHorizontal = transform.right * xMov;
- Vector3 movVertical = transform.forward * zMov;
- Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
- if(velocity != Vector3.zero)
- {
- rb.MovePosition(rb.position + velocity * Time.deltaTime);
- }
- //Camera
- float yRot = Input.GetAxisRaw("Mouse X");
- float xRot = Input.GetAxisRaw("Mouse Y");
- Vector3 yRotation = new Vector3(0, yRot, 0) * sensitivity;
- Vector3 xRotation = new Vector3(xRot, 0, 0) * sensitivity;
- rb.MoveRotation(rb.rotation * Quaternion.Euler (yRotation));
- mainCam.transform.Rotate(-xRotation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment