Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- [RequireComponent (typeof (CharacterController))]
- public class PlayerController : MonoBehaviour
- {
- // Handling Variables:
- public float rotationSpeed = 450; // Rotation Speed
- public float walkSpeed = 5; // Walking Speed
- public float runSpeed = 8; // Running Speed
- // System Variable:
- private CharacterController controller;
- // Components of Movement:
- private Quaternion targetRotation;
- void Start()
- {
- controller = GetComponent<CharacterController>();
- }
- void Update()
- {
- Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
- if (input != Vector3.zero)
- {
- targetRotation = Quaternion.LookRotation(input);
- transform.rotation = Quaternion.LookRotation(input);
- transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
- }
- // Setup the Vector3 motion:
- Vector3 motion = input.normalized;
- input *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7f : 1;
- input *= (Input.GetButton("Run")) ? runSpeed : walkSpeed;
- motion += Vector3.up * -7;
- controller.Move(motion * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment