SHARE
TWEET

Untitled

a guest Jun 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using Valve.VR.InteractionSystem;
  4.  
  5. namespace Valve.VR.InteractionSystem.Sample
  6. {
  7.     public class JoeJeff : MonoBehaviour
  8.     {
  9.         public float animationSpeed;
  10.  
  11.         public float jumpVelocity;
  12.  
  13.         [SerializeField]
  14.         private float m_MovingTurnSpeed = 360;
  15.         [SerializeField]
  16.         private float m_StationaryTurnSpeed = 180;
  17.  
  18.         public float airControl;
  19.  
  20.         [Tooltip("The time it takes after landing a jump to slow down")]
  21.         public float frictionTime = 0.2f;
  22.  
  23.         [SerializeField]
  24.         private float footHeight = 0.1f;
  25.         [SerializeField]
  26.         private float footRadius = 0.03f;
  27.  
  28.         private RaycastHit footHit;
  29.  
  30.         private bool isGrounded;
  31.  
  32.         private float turnAmount;
  33.         private float forwardAmount;
  34.  
  35.         private float groundedTime;
  36.  
  37.         private Animator animator;
  38.  
  39.         private Vector3 input;
  40.  
  41.         private bool held;
  42.  
  43.         private new Rigidbody rigidbody;
  44.         private Interactable interactable;
  45.  
  46.         public FireSource fire;
  47.  
  48.  
  49.         private void Start()
  50.         {
  51.             animator = GetComponent<Animator>();
  52.             rigidbody = GetComponent<Rigidbody>();
  53.             interactable = GetComponent<Interactable>();
  54.             animator.speed = animationSpeed;
  55.         }
  56.  
  57.         private void Update()
  58.         {
  59.             held = interactable.attachedToHand != null;
  60.  
  61.             jumpTimer -= Time.deltaTime;
  62.  
  63.             CheckGrounded();
  64.  
  65.             rigidbody.freezeRotation = !held;
  66.  
  67.             if (held == false)
  68.                 FixRotation();
  69.         }
  70.  
  71.         private void FixRotation()
  72.         {
  73.             Vector3 eulers = transform.eulerAngles;
  74.             eulers.x = 0;
  75.             eulers.z = 0;
  76.             Quaternion targetRotation = Quaternion.Euler(eulers);
  77.  
  78.             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * (isGrounded ? 20 : 3));
  79.         }
  80.  
  81.  
  82.         public void OnAnimatorMove()
  83.         {
  84.             // we implement this function to override the default root motion.
  85.             // this allows us to modify the positional speed before it's applied.
  86.             if (Time.deltaTime > 0)
  87.             {
  88.                 Vector3 animationDelta = (animator.deltaPosition) / Time.deltaTime;
  89.  
  90.                 animationDelta = Vector3.ProjectOnPlane(animationDelta, footHit.normal);
  91.  
  92.                 if (isGrounded && jumpTimer < 0)
  93.                 {
  94.                     if (groundedTime < frictionTime) //slow down when first hitting the floor after a jump
  95.                     {
  96.                         float moveFac = Mathf.InverseLerp(0, frictionTime, groundedTime);
  97.                         //print(moveFac);
  98.                         Vector3 lerpV = Vector3.Lerp(rigidbody.velocity, animationDelta, moveFac * Time.deltaTime * 30);
  99.                         animationDelta.x = lerpV.x;
  100.                         animationDelta.z = lerpV.z;
  101.                     }
  102.  
  103.                     // adding a little downward force to keep him on the floor
  104.                     animationDelta.y += -0.2f;// rb.velocity.y;
  105.                     rigidbody.velocity = animationDelta;
  106.                 }
  107.                 else
  108.                 {
  109.                     rigidbody.velocity += input * Time.deltaTime * airControl;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         public void Move(Vector3 move, bool jump)
  115.         {
  116.             input = move;
  117.             if (move.magnitude > 1f)
  118.                 move.Normalize();
  119.  
  120.             move = transform.InverseTransformDirection(move);
  121.  
  122.             turnAmount = Mathf.Atan2(move.x, move.z);
  123.             forwardAmount = move.z;
  124.  
  125.             ApplyExtraTurnRotation();
  126.  
  127.             // control and velocity handling is different when grounded and airborne:
  128.             if (isGrounded)
  129.             {
  130.                 HandleGroundedMovement(jump);
  131.             }
  132.  
  133.  
  134.             // send input and other state parameters to the animator
  135.             UpdateAnimator(move);
  136.         }
  137.  
  138.         private void UpdateAnimator(Vector3 move)
  139.         {
  140.             animator.speed = fire.isBurning ? animationSpeed * 2 : animationSpeed;
  141.             // update the animator parameters
  142.             animator.SetFloat("Forward", fire.isBurning ? 2 : forwardAmount, 0.1f, Time.deltaTime);
  143.             animator.SetFloat("Turn", turnAmount, 0.1f, Time.deltaTime);
  144.             animator.SetBool("OnGround", isGrounded);
  145.             animator.SetBool("Holding", held);
  146.  
  147.             if (!isGrounded)
  148.             {
  149.                 animator.SetFloat("FallSpeed", Mathf.Abs(rigidbody.velocity.y));
  150.                 animator.SetFloat("Jump", rigidbody.velocity.y);
  151.             }
  152.         }
  153.  
  154.         private void ApplyExtraTurnRotation()
  155.         {
  156.             // help the character turn faster (this is in addition to root rotation in the animation)
  157.             float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, forwardAmount);
  158.             transform.Rotate(0, turnAmount * turnSpeed * Time.deltaTime, 0);
  159.         }
  160.  
  161.         private void CheckGrounded()
  162.         {
  163.             isGrounded = false;
  164.             if (jumpTimer < 0 & !held) // make sure we didn't just jump
  165.             {
  166.                 isGrounded = (Physics.SphereCast(new Ray(transform.position + Vector3.up * footHeight, Vector3.down), footRadius, out footHit, footHeight - footRadius));
  167.                 if (Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(footHit.point.x, footHit.point.z)) > footRadius / 2)
  168.                 {
  169.                     isGrounded = false;
  170.                     //on slope, hit point is on edge of sphere cast
  171.                 }
  172.             }
  173.         }
  174.  
  175.  
  176.         private void FixedUpdate()
  177.         {
  178.             groundedTime += Time.fixedDeltaTime;
  179.             if (!isGrounded) groundedTime = 0; // reset timer
  180.  
  181.             if (isGrounded & !held)
  182.             {
  183.                 Debug.DrawLine(transform.position, footHit.point);
  184.  
  185.                 rigidbody.position = new Vector3(rigidbody.position.x, footHit.point.y, rigidbody.position.z);
  186.             }
  187.         }
  188.  
  189.  
  190.  
  191.         private void HandleGroundedMovement(bool jump)
  192.         {
  193.             // check whether conditions are right to allow a jump:
  194.             if (jump && isGrounded)
  195.             {
  196.                 Jump();
  197.             }
  198.         }
  199.  
  200.         private float jumpTimer;
  201.         public void Jump()
  202.         {
  203.             isGrounded = false;
  204.             jumpTimer = 0.1f;
  205.             animator.applyRootMotion = false;
  206.             rigidbody.position += Vector3.up * 0.03f;
  207.             Vector3 velocity = rigidbody.velocity;
  208.             velocity.y = jumpVelocity;
  209.             rigidbody.velocity = velocity;
  210.         }
  211.     }
  212. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top