Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MovementScript : MonoBehaviour {
- public float speed = 5;
- public float CameraSpeed = 3;
- public GameObject mainCamera;
- private Rigidbody rigidBody;
- private Vector3 movingVectorVertical;
- private Vector3 movingVectorHorizintal;
- void Start () {
- rigidBody = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- private void Update()
- {
- if (Input.GetKey("up") || Input.GetKey("w"))
- {
- movingVectorVertical = transform.forward * speed;
- }
- else if (Input.GetKey("down") || Input.GetKey("s"))
- {
- movingVectorVertical = -transform.forward * speed;
- }
- if (Input.GetKey("right") || Input.GetKey("d"))
- {
- movingVectorHorizintal = -transform.right * speed;
- }
- else if (Input.GetKey("left") || Input.GetKey("a"))
- {
- movingVectorHorizintal = transform.right * speed;
- }
- if (!(Input.GetKey("up") || Input.GetKey("w")) && !(Input.GetKey("down") || Input.GetKey("s")))
- {
- movingVectorVertical = Vector3.zero;
- }
- if (!(Input.GetKey("right") || Input.GetKey("d")) && !(Input.GetKey("left") || Input.GetKey("a")))
- {
- movingVectorHorizintal = Vector3.zero;
- }
- transform.Rotate(0, Input.GetAxis("Mouse X") * CameraSpeed, 0);
- }
- private void FixedUpdate()
- {
- var moveVec = movingVectorVertical + movingVectorHorizintal;
- rigidBody.velocity = new Vector3(moveVec.z, 0, moveVec.x);
- rigidBody.rotation =
- Quaternion.Euler(rigidBody.rotation.eulerAngles + new Vector3(0f, Input.GetAxis("Mouse X") * CameraSpeed, 0f));
- mainCamera.transform.rotation =
- Quaternion.Euler(mainCamera.transform.rotation.eulerAngles + new Vector3( -CameraSpeed * Input.GetAxis("Mouse Y"), 0f, 0f));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement