Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5.  
  6. [RequireComponent(typeof(Rigidbody))]
  7. [RequireComponent(typeof(CapsuleCollider))]
  8. public class RigidbodyPlayer : MonoBehaviour
  9. {
  10.     public float speed = 10.0f;
  11.     public float gravity = 10.0f;
  12.     public float maxVelocityChange = 10.0f;
  13.     public bool canJump = true;
  14.     public float jumpHeight = 2.0f;
  15.     private bool grounded = false;
  16.     private Rigidbody rb;
  17.    
  18.     void Awake()
  19.     {
  20.         rb = GetComponent<Rigidbody>();
  21.         rb.freezeRotation = true;
  22.         rb.useGravity = false;
  23.     }
  24.  
  25.     void FixedUpdate()
  26.     {
  27.         if (Physics.CheckCapsule(transform.position + transform.up * 0.5f, transform.position + transform. up * -0.6f, 0.5f))
  28.         {
  29.             grounded = true;
  30.         }
  31.  
  32.         // Calculate how fast we should be moving
  33.         Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  34.         targetVelocity = transform.TransformDirection(targetVelocity);
  35.         targetVelocity *= speed;
  36.  
  37.         // Apply a force that attempts to reach our target velocity
  38.         Vector3 velocity = rb.velocity;
  39.         Vector3 velocityChange = (targetVelocity - velocity);
  40.         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
  41.         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
  42.         velocityChange.y = 0;
  43.         rb.AddForce(velocityChange, ForceMode.VelocityChange);
  44.         if (grounded)
  45.         {
  46.                 // Jump
  47.             if (canJump && Input.GetButton("Jump"))
  48.             {
  49.                 rb.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
  50.             }
  51.         }
  52.  
  53.         // We apply gravity manually for more tuning control
  54.         rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));
  55.  
  56.         grounded = false;
  57.     }
  58.  
  59.  
  60.     //void OnCollisionStay()
  61.     //{
  62.     //    grounded = true;
  63.     //}
  64.  
  65.     float CalculateJumpVerticalSpeed()
  66.     {
  67.         // From the jump height and gravity we deduce the upwards speed
  68.         // for the character to reach at the apex.
  69.         return Mathf.Sqrt(2 * jumpHeight * gravity);
  70.     }
  71.  
  72.     void Start()
  73.     {
  74.         Cursor.lockState = CursorLockMode.Locked;
  75.  
  76.     }
  77.  
  78.     void Update()
  79.     {
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement