Guest User

Unity Error

a guest
Feb 20th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.92 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class SimpleSampleCharacterControl : MonoBehaviour
  5. {
  6.     private enum ControlMode
  7.     {
  8.         /// <summary>
  9.         /// Up moves the character forward, left and right turn the character gradually and down moves the character backwards
  10.         /// </summary>
  11.         Tank,
  12.         /// <summary>
  13.         /// Character freely moves in the chosen direction from the perspective of the camera
  14.         /// </summary>
  15.         Direct
  16.     }
  17.  
  18.     [SerializeField] private float m_moveSpeed = 2;
  19.     [SerializeField] private float m_turnSpeed = 200;
  20.     [SerializeField] private float m_jumpForce = 4;
  21.  
  22.     [SerializeField] private Animator m_animator = null;
  23.     [SerializeField] private Rigidbody m_rigidBody = null;
  24.  
  25.     [SerializeField] private ControlMode m_controlMode = ControlMode.Direct;
  26.  
  27.     private float m_currentV = 0;
  28.     private float m_currentH = 0;
  29.  
  30.     private readonly float m_interpolation = 10;
  31.     private readonly float m_walkScale = 0.33f;
  32.     private readonly float m_backwardsWalkScale = 0.16f;
  33.     private readonly float m_backwardRunScale = 0.66f;
  34.  
  35.     private bool m_wasGrounded;
  36.     private Vector3 m_currentDirection = Vector3.zero;
  37.  
  38.     private float m_jumpTimeStamp = 0;
  39.     private float m_minJumpInterval = 0.25f;
  40.     private bool m_jumpInput = false;
  41.  
  42.     private bool m_isGrounded;
  43.  
  44.     private List<Collider> m_collisions = new List<Collider>();
  45.  
  46.     private void Awake()
  47.     {
  48.         if (!m_animator) { gameObject.GetComponent<Animator>(); }
  49.         if (!m_rigidBody) { gameObject.GetComponent<Animator>(); }
  50.     }
  51.  
  52.  
  53.     //Added This To ensure that Character Doesnt get destoryed on load.
  54.     private void Start()
  55.     {
  56.         DontDestroyOnLoad(gameObject);
  57.     }
  58.  
  59.     private void OnCollisionEnter(Collision collision)
  60.     {
  61.         ContactPoint[] contactPoints = collision.contacts;
  62.         for (int i = 0; i < contactPoints.Length; i++)
  63.         {
  64.             if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
  65.             {
  66.                 if (!m_collisions.Contains(collision.collider))
  67.                 {
  68.                     m_collisions.Add(collision.collider);
  69.                 }
  70.                 m_isGrounded = true;
  71.             }
  72.         }
  73.     }
  74.  
  75.     private void OnCollisionStay(Collision collision)
  76.     {
  77.         ContactPoint[] contactPoints = collision.contacts;
  78.         bool validSurfaceNormal = false;
  79.         for (int i = 0; i < contactPoints.Length; i++)
  80.         {
  81.             if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
  82.             {
  83.                 validSurfaceNormal = true; break;
  84.             }
  85.         }
  86.  
  87.         if (validSurfaceNormal)
  88.         {
  89.             m_isGrounded = true;
  90.             if (!m_collisions.Contains(collision.collider))
  91.             {
  92.                 m_collisions.Add(collision.collider);
  93.             }
  94.         }
  95.         else
  96.         {
  97.             if (m_collisions.Contains(collision.collider))
  98.             {
  99.                 m_collisions.Remove(collision.collider);
  100.             }
  101.             if (m_collisions.Count == 0) { m_isGrounded = false; }
  102.         }
  103.     }
  104.  
  105.     private void OnCollisionExit(Collision collision)
  106.     {
  107.         if (m_collisions.Contains(collision.collider))
  108.         {
  109.             m_collisions.Remove(collision.collider);
  110.         }
  111.         if (m_collisions.Count == 0) { m_isGrounded = false; }
  112.     }
  113.  
  114.     private void Update()
  115.     {
  116.         //ignore Space key when NPC dialogue box is displaying, otherwise JUMP!
  117.         if (!m_jumpInput && Input.GetKey(KeyCode.Space) && !DialogueManager.GetInstance().dialogueIsPlaying)
  118.         {
  119.             m_jumpInput = true;
  120.         }
  121.     }
  122.  
  123.     private void FixedUpdate()
  124.     {
  125.         //fix the Character position when the NPC dialogue box is open.
  126.         if(DialogueManager.GetInstance().dialogueIsPlaying)
  127.         {
  128.             return;
  129.         }
  130.        
  131.         m_animator.SetBool("Grounded", m_isGrounded);
  132.  
  133.         switch (m_controlMode)
  134.         {
  135.             case ControlMode.Direct:
  136.                 DirectUpdate();
  137.                 break;
  138.  
  139.             case ControlMode.Tank:
  140.                 TankUpdate();
  141.                 break;
  142.  
  143.             default:
  144.                 Debug.LogError("Unsupported state");
  145.                 break;
  146.         }
  147.  
  148.         m_wasGrounded = m_isGrounded;
  149.         m_jumpInput = false;
  150.     }
  151.  
  152.     private void TankUpdate()
  153.     {
  154.         float v = Input.GetAxis("Vertical");
  155.         float h = Input.GetAxis("Horizontal");
  156.  
  157.         bool walk = Input.GetKey(KeyCode.LeftShift);
  158.  
  159.         if (v < 0)
  160.         {
  161.             if (walk) { v *= m_backwardsWalkScale; }
  162.             else { v *= m_backwardRunScale; }
  163.         }
  164.         else if (walk)
  165.         {
  166.             v *= m_walkScale;
  167.         }
  168.  
  169.         m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
  170.         m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);
  171.  
  172.         transform.position += transform.forward * m_currentV * m_moveSpeed * Time.deltaTime;
  173.         transform.Rotate(0, m_currentH * m_turnSpeed * Time.deltaTime, 0);
  174.  
  175.         m_animator.SetFloat("MoveSpeed", m_currentV);
  176.  
  177.         JumpingAndLanding();
  178.     }
  179.  
  180.     private void DirectUpdate()
  181.     {
  182.         float v = Input.GetAxis("Vertical");
  183.         float h = Input.GetAxis("Horizontal");
  184.  
  185.         Transform camera = Camera.main.transform;
  186.  
  187.         if (Input.GetKey(KeyCode.LeftShift))
  188.         {
  189.             v *= m_walkScale;
  190.             h *= m_walkScale;
  191.         }
  192.  
  193.         m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
  194.         m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);
  195.  
  196.         Vector3 direction = camera.forward * m_currentV + camera.right * m_currentH;
  197.  
  198.         float directionLength = direction.magnitude;
  199.         direction.y = 0;
  200.         direction = direction.normalized * directionLength;
  201.  
  202.         if (direction != Vector3.zero)
  203.         {
  204.             m_currentDirection = Vector3.Slerp(m_currentDirection, direction, Time.deltaTime * m_interpolation);
  205.  
  206.             transform.rotation = Quaternion.LookRotation(m_currentDirection);
  207.             transform.position += m_currentDirection * m_moveSpeed * Time.deltaTime;
  208.  
  209.             m_animator.SetFloat("MoveSpeed", direction.magnitude);
  210.         }
  211.  
  212.         JumpingAndLanding();
  213.     }
  214.  
  215.     private void JumpingAndLanding()
  216.     {
  217.         bool jumpCooldownOver = (Time.time - m_jumpTimeStamp) >= m_minJumpInterval;
  218.  
  219.         if (jumpCooldownOver && m_isGrounded && m_jumpInput)
  220.         {
  221.             m_jumpTimeStamp = Time.time;
  222.             m_rigidBody.AddForce(Vector3.up * m_jumpForce, ForceMode.Impulse);
  223.         }
  224.  
  225.         if (!m_wasGrounded && m_isGrounded)
  226.         {
  227.             m_animator.SetTrigger("Land");
  228.         }
  229.  
  230.         if (!m_isGrounded && m_wasGrounded)
  231.         {
  232.             m_animator.SetTrigger("Jump");
  233.         }
  234.     }
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment