Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SimpleWASDMovement : MonoBehaviour
- {
- public Rigidbody thisRigidbody;
- public Camera thisCamera;
- public float speed = 1f;
- public float jumpStrength = 1f;
- // Start is called before the first frame update
- void Start()
- {
- if (!thisRigidbody)
- thisRigidbody = GetComponent<Rigidbody>();
- if (!thisCamera)
- thisCamera = Camera.main;
- }
- // Update is called once per frame
- void Update()
- {
- MovementInput();
- LookInput();
- }
- void MovementInput()
- {
- Vector3 movementVector = Vector3.zero;
- movementVector.z -= Input.GetAxis("Horizontal");
- movementVector.x += Input.GetAxis("Vertical");
- if (Input.GetButtonDown("Jump"))
- {
- if (Physics.Raycast(transform.position, -Vector3.up, 1f))
- {
- thisRigidbody.AddForce(Vector3.up * jumpStrength, ForceMode.Impulse);
- }
- }
- if (thisCamera) // adjust the movement vector to be relative to the camera
- {
- Vector3 adjustVector = -thisCamera.transform.right;
- adjustVector.y = 0;
- movementVector = Quaternion.LookRotation(adjustVector.normalized, Vector3.up) * movementVector;
- }
- thisRigidbody.MovePosition(transform.position + movementVector * speed);
- }
- public Transform cameraPivot;
- public float cameraSpeed = 1f;
- public bool invertVertical = false;
- public float maxVertical = 60f;
- public float minVertical = 10f;
- void LookInput()
- {
- float camHorizontal = Input.GetAxis("Mouse X") * cameraSpeed;
- float camVertical = -Input.GetAxis("Mouse Y") * cameraSpeed;
- if (invertVertical)
- camVertical *= -1f;
- Vector3 newRotation = new Vector3(cameraPivot.transform.localRotation.eulerAngles.x + camVertical, cameraPivot.transform.localRotation.eulerAngles.y + camHorizontal, 0f);
- newRotation.x = Mathf.Clamp(newRotation.x, minVertical, maxVertical);
- cameraPivot.transform.localRotation = Quaternion.Euler(newRotation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment