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

Part of script not working all of a sudden.

Discussion in 'Editor & General Support' started by TheBreadPerson, Sep 4, 2021.

  1. TheBreadPerson

    TheBreadPerson

    Joined:
    Aug 6, 2021
    Posts:
    11
    I was working on the script, and I went to play test in the editor. I had just tried it out about 5 mins ago, and everything fine. Then, my third person switch and slide stopped working. I reverted the changes I made, so I was back to where I was before when I knew everything worked. Know the slide animation doesn't work, and neither does the third person. No error messages.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     public float speed = 12f;
    9.     Animator anim;
    10.     public Transform Cam;
    11.     public Vector3 camBack;
    12.     public Transform playerEyes;
    13.     public float extraGravity = 10f;
    14.     Transform player;
    15.     public float slideThreshold = 1f;
    16.     public Vector3 slideVect;
    17.     public float slideStrength = 20f;
    18.    
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.         Cursor.lockState = CursorLockMode.Locked;
    24.         anim = GetComponent<Animator>();
    25.         player = GetComponent<Transform>();
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void FixedUpdate()
    30.     {
    31.         Vector3 vel = rb.velocity;
    32.         float hoz = Input.GetAxis("Horizontal");
    33.         float vrt = Input.GetAxis("Vertical");
    34.  
    35.         rb.AddForce(Vector3.down * extraGravity);
    36.  
    37.         rb.velocity = transform.right * hoz * speed + transform.forward * vrt * speed;
    38.  
    39.         if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
    40.         {
    41.             anim.SetBool("Walking", true);
    42.            
    43.         }
    44.         else if(Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
    45.         {
    46.             anim.SetBool("Walking", false);
    47.            
    48.         }
    49.  
    50.         //Third Person
    51.         if(Input.GetKeyDown("z"))
    52.         {
    53.             Cam.localPosition = camBack;
    54.         }
    55.         else if(Input.GetKeyDown("v"))
    56.         {
    57.             Cam.localPosition = playerEyes.localPosition;
    58.         }
    59.         //Crouch/Slide
    60.         if(Input.GetButton("Crouch"))
    61.         {
    62.             if(vel.magnitude >= slideThreshold)
    63.             {
    64.                 Slide();
    65.             }
    66.             else
    67.             {
    68.                 Crouch();
    69.             }
    70.         }
    71.  
    72.        
    73.         //Stops movement when a key is not pressed
    74.         if(Input.GetButtonUp("Horizontal"))
    75.         {
    76.             rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
    77.         }
    78.  
    79.         if(Input.GetButtonUp("Vertical"))
    80.         {
    81.             rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
    82.         }
    83.  
    84.     }
    85.     //Slide function
    86.     private void Slide()
    87.     {
    88.         rb.velocity = transform.forward * slideStrength;
    89.         anim.SetBool("Sliding", true);
    90.     }
    91.     //Crouch function
    92.     private void Crouch()
    93.     {
    94.         anim.SetBool("Crouching", true);
    95.     }
    96. }
    97.  
     
  2. TheBreadPerson

    TheBreadPerson

    Joined:
    Aug 6, 2021
    Posts:
    11
    Fixed the slide, it was a problem in the animator tab. But the third person is still not working.