Advertisement
SawneyStudios

TutorialPlayer -Script

Mar 20th, 2019
6,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TutorialPlayer : MonoBehaviour
  6. {
  7.  
  8.     public GameObject Planet;
  9.     public GameObject PlayerPlaceholder;
  10.  
  11.  
  12.     public float speed = 4;
  13.     public float JumpHeight = 1.2f;
  14.  
  15.     float gravity = 100;
  16.     bool OnGround = false;
  17.  
  18.  
  19.     float distanceToGround;
  20.     Vector3 Groundnormal;
  21.  
  22.  
  23.  
  24.     private Rigidbody rb;
  25.  
  26.     // Start is called before the first frame update
  27.     void Start()
  28.     {
  29.         rb = GetComponent<Rigidbody>();
  30.         rb.freezeRotation = true;
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.  
  37.         //MOVEMENT
  38.  
  39.         float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
  40.         float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
  41.  
  42.         transform.Translate(x, 0, z);
  43.  
  44.         //Local Rotation
  45.  
  46.         if (Input.GetKey(KeyCode.E)) {
  47.  
  48.             transform.Rotate(0, 150 * Time.deltaTime, 0);
  49.         }
  50.         if (Input.GetKey(KeyCode.Q))
  51.         {
  52.  
  53.             transform.Rotate(0, -150 * Time.deltaTime, 0);
  54.         }
  55.  
  56.         //Jump
  57.  
  58.         if (Input.GetKeyDown(KeyCode.Space))
  59.         {
  60.             rb.AddForce(transform.up * 40000 * JumpHeight * Time.deltaTime);
  61.  
  62.         }
  63.  
  64.  
  65.  
  66.         //GroundControl
  67.  
  68.         RaycastHit hit = new RaycastHit();
  69.         if (Physics.Raycast(transform.position, -transform.up, out hit, 10)) {
  70.  
  71.             distanceToGround = hit.distance;
  72.             Groundnormal = hit.normal;
  73.  
  74.             if (distanceToGround <= 0.2f)
  75.             {
  76.                 OnGround = true;
  77.             }
  78.             else {
  79.                 OnGround = false;
  80.             }
  81.  
  82.  
  83.         }
  84.  
  85.  
  86.         //GRAVITY and ROTATION
  87.  
  88.         Vector3 gravDirection = (transform.position - Planet.transform.position).normalized;
  89.  
  90.         if (OnGround == false)
  91.         {
  92.             rb.AddForce(gravDirection * -gravity);
  93.  
  94.         }
  95.  
  96.         //
  97.  
  98.         Quaternion toRotation = Quaternion.FromToRotation(transform.up, Groundnormal) * transform.rotation;
  99.         transform.rotation = toRotation;
  100.  
  101.  
  102.  
  103.     }
  104.  
  105.  
  106.     //CHANGE PLANET
  107.  
  108.     private void OnTriggerEnter(Collider collision)
  109.     {
  110.         if (collision.transform != Planet.transform) {
  111.  
  112.             Planet = collision.transform.gameObject;
  113.  
  114.             Vector3 gravDirection = (transform.position - Planet.transform.position).normalized;
  115.  
  116.             Quaternion toRotation = Quaternion.FromToRotation(transform.up, gravDirection) * transform.rotation;
  117.             transform.rotation = toRotation;
  118.  
  119.             rb.velocity = Vector3.zero;
  120.             rb.AddForce(gravDirection * gravity);
  121.  
  122.  
  123.             PlayerPlaceholder.GetComponent<TutorialPlayerPlaceholder>().NewPlanet(Planet);
  124.  
  125.         }
  126.     }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement