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

Is This Animator/Movement Script Right?

Discussion in 'Scripting' started by emoney101skeleton, Jul 31, 2020.

  1. emoney101skeleton

    emoney101skeleton

    Joined:
    Jun 7, 2020
    Posts:
    12
    So I'm Setting up My Animator Script and I Don't Know I Couldn't Find Much Animator Scripts That Could Help me ad To My Movement Script So Here It Is

    Code (CSharp):
    1. using UnityEngine;
    2. public class FPSFirstPerson : MonoBehaviour
    3. {
    4. public Transform camera;
    5. public Rigidbody rb;
    6. public Animator animator;
    7. public float camRotationSpeed = 5f;
    8. public float cameraMinimumY = -60f;
    9. public float cameraMaximumY = 75f;
    10. public float rotationSmoothSpeed = 10f;
    11. public float walkSpeed = 9f;
    12. public float runSpeed = 14f;
    13. public float maxSpeed = 20f;
    14. public float jumpPower = 30f;
    15. public float _extraGravity = 45;
    16. float bodyRotationX;
    17. float camRotationY;
    18. Vector3 directionIntentX;
    19. Vector3 directionIntentY;
    20. float speed;
    21. public bool grounded;
    22. public void Awake()
    23. {
    24.      animator = GetComponent<Animator>();
    25. }
    26. void Update()
    27. {
    28.      LookRotation();
    29.      Movement();
    30.      extraGravity();
    31.      GroundCheck();
    32.      if(grounded && Input.GetButtonDown("Jump"))
    33.      {
    34.          Jump();
    35.      }
    36. }
    37. void LookRotation()
    38. {
    39.      Cursor.visible = false;
    40.      Cursor.lockState = CursorLockMode.Locked;
    41.      bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed;
    42.      camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed;
    43.      camRotationY = Mathf.Clamp(camRotationY, cameraMinimumY, cameraMaximumY);
    44.      Quaternion camTargetRotation = Quaternion.Euler(-camRotationY, 0, 0);
    45.      Quaternion bodyTargetRotation = Quaternion.Euler(0, bodyRotationX, 0);
    46.      transform.rotation = Quaternion.Lerp(transform.rotation, bodyTargetRotation, Time.deltaTime * rotationSmoothSpeed);
    47.      camera.localRotation = Quaternion.Lerp(camera.localRotation, camTargetRotation, Time.deltaTime * rotationSmoothSpeed);
    48. }
    49. void Movement()
    50. {
    51.      directionIntentX = camera.right;
    52.      directionIntentX.y = 0;
    53.      directionIntentX.Normalize();
    54.      directionIntentY = camera.forward;
    55.      directionIntentY.y = 0;
    56.      directionIntentY.Normalize();
    57.      rb.velocity = directionIntentY * Input.GetAxis("Vertical") * speed + directionIntentX * Input.GetAxis("Horizontal") * speed + Vector3.up * rb.velocity.y;
    58.      rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
    59.      if (directionIntentX != 0) animator.SetFloat("Fight Idle", true);
    60.      else animator.SetFloat("Walkingn", false);
    61.    
    62.      if (directionIntentY != 0) animator.SetFloat("Fight Idle", true);
    63.      else animator.SetFloat("Walking", false);
    64.    
    65.      if (directionIntentX != 1) animator.SetFloat("Walking", true);
    66.      else animator.SetFloat("Walking", false);
    67.    
    68.      if (directionIntentY != 1) animator.SetFloat("Walking", true);
    69.      else animator.SetFloat("Walking", false);
    70.      if (directionIntentX != 2) animator.SetFloat("Run", true);
    71.      else animator.SetFloat("Run", false);
    72.      if (directionIntentY != 2) animator.SetFloat("Run", true);
    73.      else animator.SetFloat("Run", false);
    74.      if(Input.GetKey(KeyCode.LeftShift))
    75.      {
    76.          speed = runSpeed;
    77.      }
    78.      if(!Input.GetKey(KeyCode.LeftShift))
    79.      {
    80.          speed = walkSpeed;
    81.      }
    82. }
    83. void extraGravity()
    84. {
    85.      rb.AddForce(Vector3.down * _extraGravity );
    86. }
    87. void GroundCheck()
    88. {
    89.      RaycastHit groundHit;
    90.      grounded = Physics.Raycast(transform.position, -transform.up, out  groundHit, 1.25f);
    91. }
    92. void Jump()
    93. {
    94.      rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
    95. }
    96. }
    So I Just Wanted To Have Anyone Correct It If, I got Anything Wrong Or Something to Change Any Help Given Thank You
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    There is no such thing as a "correct" script or an "incorrect" script. There are only scripts that do what you intend and scripts that do not. Without knowing exactly what you want the script to do, it's really hard to say.
     
    Yanne065 likes this.
  3. emoney101skeleton

    emoney101skeleton

    Joined:
    Jun 7, 2020
    Posts:
    12
    Ok I'm Making Sure On My Animator Script Attached to My Movement One