Advertisement
Guest User

[Unity 5] C# Third Person MMO Style Character Controller

a guest
Sep 24th, 2016
7,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MMOCharacterController : MonoBehaviour {
  5.  
  6.     public Transform playerCam, character, centerPoint;
  7.  
  8.     private float mouseX, mouseY;
  9.     public float mouseSensitivity = 10f;
  10.     public float mouseYPosition = 1f;
  11.  
  12.     private float moveFB, moveLR;
  13.     public float moveSpeed = 2f;
  14.  
  15.     private float zoom;
  16.     public float zoomSpeed = 2;
  17.  
  18.     public float zoomMin = -2f;
  19.     public float zoomMax = -10f;
  20.  
  21.     public float rotationSpeed = 5f;
  22.  
  23.  
  24.  
  25.     // Use this for initialization
  26.     void Start () {
  27.    
  28.         zoom = -3;
  29.  
  30.     }
  31.    
  32.     // Update is called once per frame
  33.     void Update () {
  34.  
  35.         zoom += Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;
  36.  
  37.         if (zoom > zoomMin)
  38.             zoom = zoomMin;
  39.  
  40.         if (zoom < zoomMax)
  41.             zoom = zoomMax;
  42.  
  43.         playerCam.transform.localPosition = new Vector3 (0, 0, zoom);
  44.  
  45.         if (Input.GetMouseButton (1)) {
  46.             mouseX += Input.GetAxis ("Mouse X");
  47.             mouseY -= Input.GetAxis ("Mouse Y");
  48.         }
  49.  
  50.         mouseY = Mathf.Clamp (mouseY, -60f, 60f);
  51.         playerCam.LookAt (centerPoint);
  52.         centerPoint.localRotation = Quaternion.Euler (mouseY, mouseX, 0);
  53.  
  54.         moveFB = Input.GetAxis ("Vertical") * moveSpeed;
  55.         moveLR = Input.GetAxis ("Horizontal") * moveSpeed;
  56.  
  57.         Vector3 movement = new Vector3 (moveLR, 0, moveFB);
  58.         movement = character.rotation * movement;
  59.         character.GetComponent<CharacterController> ().Move (movement * Time.deltaTime);
  60.         centerPoint.position = new Vector3 (character.position.x, character.position.y + mouseYPosition, character.position.z);
  61.  
  62.         if (Input.GetAxis ("Vertical") > 0 | Input.GetAxis ("Vertical") < 0) {
  63.  
  64.             Quaternion turnAngle = Quaternion.Euler (0, centerPoint.eulerAngles.y, 0);
  65.  
  66.             character.rotation = Quaternion.Slerp (character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
  67.  
  68.         }
  69.    
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement