Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help me error script

Discussion in 'Scripting' started by fakeman14, Mar 31, 2017.

  1. fakeman14

    fakeman14

    Joined:
    Mar 31, 2017
    Posts:
    3
    Code (CSharp):
    1. using System;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5.  
    6. namespace UnityStandardAssets.Characters.FirstPerson
    7. {
    8.     [RequireComponent(typeof (Rigidbody))]
    9.     [RequireComponent(typeof (CapsuleCollider))]
    10.     public class RigidbodyFirstPersonController : MonoBehaviour
    11.     {
    12.         [Serializable]
    13.         public class MovementSettings
    14.         {
    15.             public float ForwardSpeed = 8.0f;   // Speed when walking forward
    16.             public float BackwardSpeed = 4.0f;  // Speed when walking backwards
    17.             public float StrafeSpeed = 4.0f;    // Speed when walking sideways
    18.             public float RunMultiplier = 2.0f;   // Speed when sprinting
    19.             public KeyCode RunKey = KeyCode.LeftShift;
    20.             public Slider staminaBar;
    21.             public float JumpForce = 30f;
    22.             public AnimationCurve SlopeCurveModifier = new AnimationCurve(new Keyframe(-90.0f, 1.0f), new Keyframe(0.0f, 1.0f), new Keyframe(90.0f, 0.0f));
    23.             [HideInInspector] public float CurrentTargetSpeed = 8f;
    24.  
    25. #if !MOBILE_INPUT
    26.             private bool m_Running;
    27. #endif
    28.  
    29.             public void UpdateDesiredTargetSpeed(Vector2 input)
    30.             {
    31.                 if (input == Vector2.zero) return;
    32.                 if (input.x > 0 || input.x < 0)
    33.                 {
    34.                     //strafe
    35.                     CurrentTargetSpeed = StrafeSpeed;
    36.                 }
    37.                 if (input.y < 0)
    38.                 {
    39.                     //backwards
    40.                     CurrentTargetSpeed = BackwardSpeed;
    41.                 }
    42.                 if (input.y > 0)
    43.                 {
    44.                     //forwards
    45.                     //handled last as if strafing and moving forward at the same time forwards speed should take precedence
    46.                     CurrentTargetSpeed = ForwardSpeed;
    47.                 }
    48. #if !MOBILE_INPUT
    49.                 if (Input.GetKey(RunKey) && staminaBar.value > 30)
    50.                 {
    51.                     CurrentTargetSpeed *= RunMultiplier;
    52.                     m_Running = true;
    53.                 }
    54.                 else
    55.                 {
    56.                     m_Running = false;
    57.                 }
    58. #endif
    59.             }
    60.  
    61. #if !MOBILE_INPUT
    62.             public bool Running
    63.             {
    64.                 get { return m_Running; }
    65.             }
    66. #endif
    67.         }
    68.  
    69.  
    70.         [Serializable]
    71.         public class AdvancedSettings
    72.         {
    73.             public float groundCheckDistance = 0.01f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
    74.             public float stickToGroundHelperDistance = 0.5f; // stops the character
    75.             public float slowDownRate = 20f; // rate at which the controller comes to a stop when there is no input
    76.             public bool airControl; // can the user control the direction that is being moved in the air
    77.             [Tooltip("set it to 0.1 or more if you get stuck in wall")]
    78.             public float shellOffset; //reduce the radius by that ratio to avoid getting stuck in wall (a value of 0.1f is nice)
    79.         }
    80.  
    81.  
    82.         public Camera cam;
    83.         public MovementSettings movementSettings = new MovementSettings();
    84.         public MouseLook mouseLook = new MouseLook();
    85.         public AdvancedSettings advancedSettings = new AdvancedSettings();
    86.  
    87.  
    88.         private Rigidbody m_RigidBody;
    89.         private CapsuleCollider m_Capsule;
    90.         private float m_YRotation;
    91.         private Vector3 m_GroundContactNormal;
    92.         private bool m_Jump, m_PreviouslyGrounded, m_Jumping, m_IsGrounded;
    93.  
    94.  
    95.  
    96.         public Vector3 Velocity
    97.         {
    98.             get { return m_RigidBody.velocity; }
    99.         }
    100.  
    101.         public bool Grounded
    102.         {
    103.             get { return m_IsGrounded; }
    104.         }
    105.  
    106.         public bool Jumping
    107.         {
    108.             get { return m_Jumping; }
    109.         }
    110.  
    111.         public bool Running
    112.         {
    113.             get
    114.             {
    115. #if !MOBILE_INPUT
    116.                 return movementSettings.Running;
    117. #else
    118.                 return false;
    119. #endif
    120.             }
    121.         }
    122.  
    123.  
    124.         private void Start()
    125.         {
    126.             m_RigidBody = GetComponent<Rigidbody>();
    127.             m_Capsule = GetComponent<CapsuleCollider>();
    128.             mouseLook.Init (transform, cam.transform);
    129.         }
    130.  
    131.  
    132.         private void Update()
    133.         {
    134.             RotateView();
    135.  
    136.             if (CrossPlatformInputManager.GetButtonDown ("Jump") && !m_Jump)
    137.             {
    138.                 if (staminaBar.value> 30) {  
    139.                     m_Jump = true;
    140.                 }
    141.             }  
    142.  
    143.         }
    144.  
    145.         private void FixedUpdate()
    146.         {
    147.             GroundCheck();
    148.             Vector2 input = GetInput();
    149.  
    150.             if ((Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon) && (advancedSettings.airControl || m_IsGrounded))
    151.             {
    152.                 // always move along the camera forward as it is the direction that it being aimed at
    153.                 Vector3 desiredMove = cam.transform.forward*input.y + cam.transform.right*input.x;
    154.                 desiredMove = Vector3.ProjectOnPlane(desiredMove, m_GroundContactNormal).normalized;
    155.  
    156.                 desiredMove.x = desiredMove.x*movementSettings.CurrentTargetSpeed;
    157.                 desiredMove.z = desiredMove.z*movementSettings.CurrentTargetSpeed;
    158.                 desiredMove.y = desiredMove.y*movementSettings.CurrentTargetSpeed;
    159.                 if (m_RigidBody.velocity.sqrMagnitude <
    160.                     (movementSettings.CurrentTargetSpeed*movementSettings.CurrentTargetSpeed))
    161.                 {
    162.                     m_RigidBody.AddForce(desiredMove*SlopeMultiplier(), ForceMode.Impulse);
    163.                 }
    164.             }
    165.  
    166.             if (m_IsGrounded)
    167.             {
    168.                 m_RigidBody.drag = 5f;
    169.  
    170.                 if (m_Jump)
    171.                 {
    172.                     staminaBar.value -= 30;
    173.                     m_RigidBody.drag = 0f;
    174.                     m_RigidBody.velocity = new Vector3(m_RigidBody.velocity.x, 0f, m_RigidBody.velocity.z);
    175.                     m_RigidBody.AddForce(new Vector3(0f, movementSettings.JumpForce, 0f), ForceMode.Impulse);
    176.                     m_Jumping = true;
    177.                 }
    178.  
    179.                 if (!m_Jumping && Mathf.Abs(input.x) < float.Epsilon && Mathf.Abs(input.y) < float.Epsilon && m_RigidBody.velocity.magnitude < 1f)
    180.                 {
    181.                     m_RigidBody.Sleep();
    182.                 }
    183.             }
    184.             else
    185.             {
    186.                 m_RigidBody.drag = 0f;
    187.                 if (m_PreviouslyGrounded && !m_Jumping)
    188.                 {
    189.                     StickToGroundHelper();
    190.                 }
    191.             }
    192.             m_Jump = false;
    193.         }
    194.  
    195.  
    196.         private float SlopeMultiplier()
    197.         {
    198.             float angle = Vector3.Angle(m_GroundContactNormal, Vector3.up);
    199.             return movementSettings.SlopeCurveModifier.Evaluate(angle);
    200.         }
    201.  
    202.  
    203.         private void StickToGroundHelper()
    204.         {
    205.             RaycastHit hitInfo;
    206.             if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
    207.                                    ((m_Capsule.height/2f) - m_Capsule.radius) +
    208.                                    advancedSettings.stickToGroundHelperDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
    209.             {
    210.                 if (Mathf.Abs(Vector3.Angle(hitInfo.normal, Vector3.up)) < 85f)
    211.                 {
    212.                     m_RigidBody.velocity = Vector3.ProjectOnPlane(m_RigidBody.velocity, hitInfo.normal);
    213.                 }
    214.             }
    215.         }
    216.  
    217.  
    218.         private Vector2 GetInput()
    219.         {
    220.            
    221.             Vector2 input = new Vector2
    222.                 {
    223.                     x = CrossPlatformInputManager.GetAxis("Horizontal"),
    224.                     y = CrossPlatformInputManager.GetAxis("Vertical")
    225.                 };
    226.             movementSettings.UpdateDesiredTargetSpeed(input);
    227.             return input;
    228.         }
    229.  
    230.  
    231.         private void RotateView()
    232.         {
    233.             //avoids the mouse looking if the game is effectively paused
    234.             if (Mathf.Abs(Time.timeScale) < float.Epsilon) return;
    235.  
    236.             // get the rotation before it's changed
    237.             float oldYRotation = transform.eulerAngles.y;
    238.  
    239.             mouseLook.LookRotation (transform, cam.transform);
    240.  
    241.             if (m_IsGrounded || advancedSettings.airControl)
    242.             {
    243.                 // Rotate the rigidbody velocity to match the new direction that the character is looking
    244.                 Quaternion velRotation = Quaternion.AngleAxis(transform.eulerAngles.y - oldYRotation, Vector3.up);
    245.                 m_RigidBody.velocity = velRotation*m_RigidBody.velocity;
    246.             }
    247.         }
    248.  
    249.         /// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
    250.         private void GroundCheck()
    251.         {
    252.             m_PreviouslyGrounded = m_IsGrounded;
    253.             RaycastHit hitInfo;
    254.             if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
    255.                                    ((m_Capsule.height/2f) - m_Capsule.radius) + advancedSettings.groundCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
    256.             {
    257.                 m_IsGrounded = true;
    258.                 m_GroundContactNormal = hitInfo.normal;
    259.             }
    260.             else
    261.             {
    262.                 m_IsGrounded = false;
    263.                 m_GroundContactNormal = Vector3.up;
    264.             }
    265.             if (!m_PreviouslyGrounded && m_IsGrounded && m_Jumping)
    266.             {
    267.                 m_Jumping = false;
    268.             }
    269.         }
    270.     }
    271. }
    272.  
     
    Last edited: Mar 31, 2017
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
  3. fakeman14

    fakeman14

    Joined:
    Mar 31, 2017
    Posts:
    3
    FIXED
     
  4. fakeman14

    fakeman14

    Joined:
    Mar 31, 2017
    Posts:
    3