Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Mathematics;
- using UnityEngine;
- public class WallFlip : MonoBehaviour
- {
- public float Speed = 40f;
- public float JumpHight = 1f;
- public Vector3 FreezRot = Vector3.up;
- float LocalGravity = 9.8f;
- float Jump = 0;
- Vector3 MoveDirection = Vector3.zero;
- Vector3 LookDirection;
- Rigidbody Body;
- void Start()
- {
- Body = this.GetComponent<Rigidbody>();
- Body.useGravity = false; //Remove gravity we will make our own
- }
- void Update()
- {
- //Raycast infront of the player to get the wall
- if (Input.GetButtonUp("Fire1"))
- {
- RaycastHit RayData;
- if (Physics.Raycast(this.transform.position, this.transform.forward,out RayData))
- {
- this.transform.up = RayData.normal;
- FreezRot = RayData.normal;
- }
- }
- //For backup lets make a button that resets the gravity to normal
- if (Input.GetButtonUp("Fire2"))
- {
- this.transform.up = Vector3.up;
- FreezRot = Vector3.up;
- }
- //Jump
- if (Input.GetButtonUp("Jump"))
- {
- //We make it a timer to get a nice curve
- Jump = JumpHight/ LocalGravity;
- }
- //This is where we get the input for move, input should be collected during update
- MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- }
- //Physics should be done in fixed update
- private void FixedUpdate()
- {
- this.transform.up = FreezRot;
- //Lets make the velocity
- Body.velocity = this.transform.forward * MoveDirection.z * Speed;
- Body.velocity += this.transform.right * MoveDirection.x * Speed; //You can instead use X to rotate
- LookDirection = Body.velocity;//We can use it to look in the moving direction
- //Do gravity and jump
- if (Jump == 0)
- {
- Body.velocity += -FreezRot * LocalGravity; //Finally add gravity
- }
- else
- {
- Jump -= 1f * Time.deltaTime;
- Jump = Mathf.Max(Jump, 0);//Clamp to zero
- print(Jump);
- Body.velocity += FreezRot * LocalGravity;
- }
- //Just make it look in the moving direction
- if (LookDirection.magnitude > 0f)
- {
- this.transform.LookAt(this.transform.position + LookDirection, this.transform.up);
- }
- }
- }
Add Comment
Please, Sign In to add comment