Search Unity

Object Player Movement stops working after Restarting Unity

Discussion in 'Editor & General Support' started by pebblechan, Nov 18, 2019.

  1. pebblechan

    pebblechan

    Joined:
    Nov 18, 2019
    Posts:
    4
    Hi all,

    As the question title says: my object which is a controllable player's movement was working until I closed and re-opened Unity.

    This has happened twice so far. After the first time, I simply deleted the player object and then reattached all the scripts... which worked. But then, I closed Unity and reopened it, and the problem happened again!

    These are all the scripts I've attached to the player "Chicken":

    MOVEMENT:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.  
    6.  
    7.      public float playerSpeed = 5.0f;
    8.      public float smooth = 5.0f;
    9.      public float tiltAngle = 90;
    10.      public float speed;
    11.    
    12.      void Start ()
    13.      {
    14.          //player spawns here
    15.      
    16.          transform.position = new Vector3(385, 0, 318);
    17.      }
    18.    
    19.  
    20.      // Update is called once per frame
    21.      void Update (){
    22.        
    23. if(Input.GetKey(KeyCode.W)){
    24.       transform.Translate(0,0,5*Time.deltaTime);
    25.       // Rotate the cube by converting the angles into a quaternion.
    26.          transform.rotation = Quaternion.Euler(0, -90, 0);
    27.    
    28.   }if(Input.GetKey(KeyCode.S)){
    29.       transform.Translate(new Vector3(0,0,5)*Time.deltaTime);
    30.       transform.rotation = Quaternion.Euler(0, 90, 0);
    31.    
    32.   }if(Input.GetKey(KeyCode.D)){
    33.       transform.Translate(0,0,5*Time.deltaTime);
    34.       // Rotate the cube by converting the angles into a quaternion.
    35.          transform.rotation = Quaternion.Euler(0, 0, 0);
    36.  
    37.   }if(Input.GetKey(KeyCode.A)){
    38.       transform.Translate(new Vector3(0,0,5)*Time.deltaTime);
    39.       // Rotate the cube by converting the angles into a quaternion.
    40.          transform.rotation = Quaternion.Euler(0, 180, 0);
    41.    
    42.   }
    43.  
    44.    
    45.  
    46.   }
    47.  
    48.      }
    JUMP:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Jump : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     private CharacterController controller;
    9.     private float verticalVelocity;
    10.     private float gravity = 14f;
    11.     private float jumpForce = 5.0f;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         controller = GetComponent<CharacterController>();
    18.         anim = GetComponent<Animator>();
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input. GetKeyDown(KeyCode.Space))
    26.         {
    27.             anim.Play("Run In Place");
    28.         }
    29.      
    30.         if (Input. GetKeyUp(KeyCode.Space))
    31.         {
    32.             anim.Play("Idle");
    33.         }
    34.      
    35.         if(controller.isGrounded){
    36.      verticalVelocity = -gravity * Time.deltaTime;
    37.      if(Input.GetKeyDown(KeyCode.Space))
    38.      {
    39.          verticalVelocity = jumpForce;
    40.      }
    41. } else {
    42.      verticalVelocity -= gravity * Time.deltaTime;
    43. } Vector3 moveVector = new Vector3 (0,verticalVelocity,0);
    44. controller.Move(moveVector * Time.deltaTime);
    45.     }
    46. }
    47.  
    ANIMATIONS:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class animController : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (Input. GetKeyDown(KeyCode.W))
    18.         {
    19.             anim.Play("Walk W Root");
    20.         }
    21.      
    22.          if (Input. GetKeyDown(KeyCode.S))
    23.         {
    24.             anim.Play("Walk W Root");
    25.         }
    26.      
    27.          if (Input. GetKeyDown(KeyCode.A))
    28.         {
    29.             anim.Play("Walk W Root");
    30.         }
    31.      
    32.          if (Input. GetKeyDown(KeyCode.D))
    33.         {
    34.             anim.Play("Walk W Root");
    35.         }
    36.         else if (Input. GetKeyUp(KeyCode.W))
    37.         {
    38.             anim.Play("Idle");
    39.         }
    40.         else if (Input. GetKeyUp(KeyCode.S))
    41.         {
    42.             anim.Play("Idle");
    43.         }
    44.         else if (Input. GetKeyUp(KeyCode.A))
    45.         {
    46.             anim.Play("Idle");
    47.         }
    48.         else if (Input. GetKeyUp(KeyCode.D))
    49.         {
    50.             anim.Play("Idle");
    51.         }
    52.     }
    53. }
    54.  

    Here is the Inspector bar for "Chicken:



    I'm still a beginner with Unity so any response would be greatly appreciated! Thank you!
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  3. pebblechan

    pebblechan

    Joined:
    Nov 18, 2019
    Posts:
    4
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Everywhere! Then follow them in the console, and you'll learn what code is executing.

    Debug.Log("I am here now");
    ...
    Debug.Log("Now I am here...");

    And add some like in the video where you print out your variables to make sure they contain the values that you expect.
     
  5. pebblechan

    pebblechan

    Joined:
    Nov 18, 2019
    Posts:
    4
    Hi Jeff,

    I'm sorry - I am a total beginner. None of the tutorials are making sense to me. Haha! Could you please walk me through this?

    I wrote this Debug.Log line in my code: Debug.Log("X= " + transform.position.x);

    The log just simply stated that "X = 385". That doesn't help me! I know the Chicken is at X = 385. My issue is that when I press on WASD, the Chicken's position values do not change! I need to figure out why that is.
    What exactly is the use of Debugging for my particular case?

    Thank you.
     
    Last edited: Nov 20, 2019
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You would follow the logic path with your debug statements. First, see if "W" is being recognized as you expect, so put your first debug statement there. And locate in the code where the position values are set (look for the = sign with the variable name on the left hand side), and put debug statements there too.
     
  7. pebblechan

    pebblechan

    Joined:
    Nov 18, 2019
    Posts:
    4
    Hi Jeff,

    Thank you for responding always! I've debugged the whole movement script. WASD are all being recognized as pressed (which I expected, because I coded that the Chicken rotate with the different directions when WAS or D are pressed, which works as wanted, when WASD are pressed).

    And, the X & Z values are changing but in very small decimals (i.e. Z = 317.9184 and Z = 317.9131) then, once it hits a certain value it returns to the original one, like a loop. So, Unity is recognizing that buttons are being pressed, and that Z or X values need to change, but why is the change sooo small? And why is it looping? I don't understand!

    This is so strange! My code works when I delete the whole Chicken, re-add, reapply the scripts and play. But, the moment I close and reopen Unity, the script breaks and the Chicken doesn't move!
     
    Last edited: Nov 21, 2019