Advertisement
SebastianLague

First Person Controller (Spherical Worlds)

Oct 3rd, 2014
9,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. // Unity Tutorial Here: http://youtu.be/TicipSVT-T8
  2. // Sebastian Lague
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. [RequireComponent (typeof (Rigidbody))]
  8. public class GravityBody : MonoBehaviour {
  9.  
  10.     GravityAttractor planet;
  11.  
  12.     void Awake () {
  13.         planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
  14.  
  15.         // Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
  16.         rigidbody.useGravity = false;
  17.         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
  18.     }
  19.    
  20.     void FixedUpdate () {
  21.         // Allow this body to be influenced by planet's gravity
  22.         planet.Attract(transform);
  23.     }
  24. }
  25.  
  26. using UnityEngine;
  27. using System.Collections;
  28.  
  29. public class GravityAttractor : MonoBehaviour {
  30.  
  31.     public float gravity = -9.8f;
  32.  
  33.  
  34.     public void Attract(Transform body) {
  35.         Vector3 gravityUp = (body.position - transform.position).normalized;
  36.         Vector3 localUp = body.up;
  37.  
  38.         // Apply downwards gravity to body
  39.         body.rigidbody.AddForce(gravityUp * gravity);
  40.         // Allign bodies up axis with the centre of planet
  41.         body.rotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
  42.     }  
  43. }
  44.  
  45. using UnityEngine;
  46. using System.Collections;
  47.  
  48. [RequireComponent (typeof (GravityBody))]
  49. public class FirstPersonController : MonoBehaviour {
  50.  
  51.     // public vars
  52.     public float mouseSensitivityX = 250;
  53.     public float mouseSensitivityY = 250;
  54.     public float walkSpeed = 6;
  55.     public float jumpForce = 220;
  56.     public LayerMask groundedMask;
  57.  
  58.     // System vars
  59.     bool grounded;
  60.     Vector3 moveAmount;
  61.     Vector3 smoothMoveVelocity;
  62.     float verticalLookRotation;
  63.     Transform cameraTransform;
  64.  
  65.  
  66.     void Awake() {
  67.         Screen.lockCursor = true;
  68.         cameraTransform = Camera.main.transform;
  69.     }
  70.  
  71.     void Update() {
  72.  
  73.         // Look rotation:
  74.         transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
  75.         verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
  76.         verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
  77.         cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
  78.  
  79.         // Calculate movement:
  80.         float inputX = Input.GetAxisRaw("Horizontal");
  81.         float inputY = Input.GetAxisRaw("Vertical");
  82.  
  83.         Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
  84.         Vector3 targetMoveAmount = moveDir * walkSpeed;
  85.         moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
  86.  
  87.         // Jump
  88.         if (Input.GetButtonDown("Jump")) {
  89.             if (grounded) {
  90.                 rigidbody.AddForce(transform.up * jumpForce);
  91.             }
  92.         }
  93.  
  94.         // Grounded check
  95.         Ray ray = new Ray(transform.position, -transform.up);
  96.         RaycastHit hit;
  97.  
  98.         if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
  99.             grounded = true;
  100.         }
  101.         else {
  102.             grounded = false;
  103.         }
  104.  
  105.     }
  106.  
  107.     void FixedUpdate() {
  108.         // Apply movement to rigidbody
  109.         Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
  110.         rigidbody.MovePosition(rigidbody.position + localMove);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement