MysteryGM

Unity_LocalGravitySwitch

Jun 12th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.Mathematics;
  4. using UnityEngine;
  5.  
  6. public class WallFlip : MonoBehaviour
  7. {
  8.     public float Speed = 40f;
  9.     public float JumpHight = 1f;
  10.     public Vector3 FreezRot = Vector3.up;
  11.  
  12.  
  13.     float LocalGravity = 9.8f;
  14.     float Jump = 0;
  15.     Vector3 MoveDirection = Vector3.zero;
  16.     Vector3 LookDirection;
  17.     Rigidbody Body;
  18.  
  19.     void Start()
  20.     {
  21.         Body = this.GetComponent<Rigidbody>();
  22.         Body.useGravity = false; //Remove gravity we will make our own
  23.     }
  24.  
  25.  
  26.     void Update()
  27.     {
  28.         //Raycast infront of the player to get the wall
  29.         if (Input.GetButtonUp("Fire1"))
  30.         {
  31.             RaycastHit RayData;
  32.             if (Physics.Raycast(this.transform.position, this.transform.forward,out RayData))
  33.             {
  34.                 this.transform.up = RayData.normal;
  35.                 FreezRot = RayData.normal;
  36.             }
  37.         }
  38.  
  39.         //For backup lets make a button that resets the gravity to normal
  40.         if (Input.GetButtonUp("Fire2"))
  41.         {
  42.             this.transform.up = Vector3.up;
  43.             FreezRot = Vector3.up;
  44.         }
  45.  
  46.         //Jump
  47.         if (Input.GetButtonUp("Jump"))
  48.         {
  49.             //We make it a timer to get a nice curve
  50.             Jump = JumpHight/ LocalGravity;
  51.         }
  52.  
  53.         //This is where we get the input for move, input should be collected during update
  54.         MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  55.     }
  56.  
  57.     //Physics should be done in fixed update
  58.     private void FixedUpdate()
  59.     {
  60.         this.transform.up = FreezRot;
  61.  
  62.         //Lets make the velocity
  63.         Body.velocity = this.transform.forward * MoveDirection.z * Speed;
  64.         Body.velocity += this.transform.right * MoveDirection.x * Speed; //You can instead use X to rotate
  65.  
  66.         LookDirection = Body.velocity;//We can use it to look in the moving direction
  67.  
  68.         //Do gravity and jump
  69.         if (Jump == 0)
  70.         {
  71.             Body.velocity += -FreezRot * LocalGravity; //Finally add gravity
  72.         }
  73.         else
  74.         {
  75.             Jump -= 1f * Time.deltaTime;
  76.             Jump = Mathf.Max(Jump, 0);//Clamp to zero
  77.             print(Jump);
  78.             Body.velocity += FreezRot * LocalGravity;
  79.         }
  80.  
  81.         //Just make it look in the moving direction
  82.         if (LookDirection.magnitude > 0f)
  83.         {
  84.             this.transform.LookAt(this.transform.position + LookDirection, this.transform.up);
  85.         }
  86.     }
  87. }
Add Comment
Please, Sign In to add comment