Search Unity

Make character move

Discussion in 'Scripting' started by xeonheart, Jul 25, 2018.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello, I have been looking online about this, of course its great that there is more than 1 way to do this, but here is my question, the C# code below doesn't work on my Barbarian asset to move using A,S,W,D:

    Code (CSharp):
    1.     public Rigidbody rb;
    2.     public float forwardforce = 50f;
    3.     public float sidewaysforce = 50f;
    4.     public bool MoveLeft = false;
    5.     public bool MoveRight = false;
    6.     public bool MoveForward = false;
    7.     public bool MoveBack = false;
    8.    
    9. void Update ()
    10.     {
    11. if (Input.GetKeyUp(KeyCode.S))
    12.         {
    13.             MoveBack = false;
    14.         }
    15.         if (MoveRight == true)
    16.         {
    17.             rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0);
    18.         }
    19.  
    20.         if (MoveLeft == true)
    21.         {
    22.             rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0);
    23.         }
    24.  
    25.         if (MoveForward == true)
    26.         {
    27.             rb.AddForce(0, 0, forwardforce * Time.deltaTime);
    28.         }
    29.  
    30.         if (MoveBack == true)
    31.         {
    32.             rb.AddForce(0, 0, -forwardforce * Time.deltaTime);
    33.         }
    34. }
    35.  
    36. void FixedUpdate()
    37.     {
    38.         if (Input.GetKeyDown(KeyCode.D))
    39.         {
    40.             MoveRight = true;
    41.         }
    42.         if (Input.GetKeyUp(KeyCode.D))
    43.         {
    44.             MoveRight = false;
    45.         }
    46.  
    47.  
    48.         if (Input.GetKeyDown(KeyCode.W))
    49.         {
    50.             MoveForward = true;
    51.         }
    52.         if (Input.GetKeyUp(KeyCode.W))
    53.         {
    54.             MoveForward = false;
    55.         }
    56.  
    57.         if (Input.GetKeyDown(KeyCode.S))
    58.         {
    59.             MoveBack = true;
    60.         }
    61.         if (Input.GetKeyUp(KeyCode.S))
    62.         {
    63.             MoveBack = false;
    64.         }
    65.  
    66.         if (Input.GetKeyDown(KeyCode.A))
    67.         {
    68.             MoveLeft = true;
    69.         }
    70.         if (Input.GetKeyUp(KeyCode.A))
    71.         {
    72.             MoveLeft = false;
    73.         }
    74. }
    75.  
    so above doesnt work, however for the testing, i did add the same code to a cube on top of another cube, the second was acting as the ground, and the first was the character, and the above code work... now i found another tutorial on moving character:


    code is below:
    Code (CSharp):
    1. public Rigidbody rb;
    2. public float speed = 18;
    3.  
    4. void Update ()
    5.     {
    6.  
    7.         float haxis = Input.GetAxis("Horizontal");
    8.         float vaxis = Input.GetAxis("Vertical");
    9.  
    10.         Vector3 movement = new Vector3(haxis, 0, vaxis) * speed * Time.deltaTime;
    11.  
    12.         rb.MovePosition(transform.position + movement);
    13.  
    14.  
    15.     }
    and this worked perfectly... my question is, why? is it because i was using fixedupdate method? I remember when watching brackeys tutorial is to do it like the first one, using a mix of update and fixed update because of frame rate... but not sure if its good practice or recommended? any thoughts on why and what would you guys do? thanks in advance
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Typically, FixedUpdate is more for pure physics calculations where absolute time steps are important to maintain the integrity of things like mathematical equations.

    The Update method is used for pretty much everything else and is linked directly to frame rate. This allows for things to be smoothed to improve the visuals of animations, for example.

    There is nothing in the code, as pasted above, that would really seem to fit into FixedUpdate.
     
    xeonheart likes this.
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    In the following code, you press on the S key, and any number of directions can happen, probably not what you want. If I press on the S key, I don't want to move right. I do see that you are setting variables in FixedUpdate, I think you may want to revisit this logic.

    1. if (Input.GetKeyUp(KeyCode.S))
    2. {
    3. MoveBack = false;
    4. }
    5. if (MoveRight == true)
    6. {
    7. rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0);
    8. }
     
    xeonheart likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Harping on FixedUpdate a bit more, Input.GetKeyDown and Input.GetKeyUp only return true on the frame in which the key was pressed or released. You can have multiple frames between each call to FixedUpdate though, so it is possible that the player can press or release a key and these Input calls can return false when you expect them to return true. These should always be used in Update or anywhere you are sure is getting called once per frame.

    If you really think you need to get key presses in FixedUpdate use Input.GetKey, which returns true the entire time the key is being held down. These are far less likely to be missed during a FixedUpdate.
     
    xeonheart likes this.
  5. creepi

    creepi

    Joined:
    Nov 10, 2015
    Posts:
    34
    Aloha!
    i have a problem with the movement and think, ask here. It is maybe the right thread for it.

    I have try with transform.translate, rb.velocity and with rb.addforce, rb.moveposition
    Try it with Update and FixedUpdate, with *time.deltatime, time.smoothdeltatime and time.fixeddeltatime... all have the same problem.

    Its working when the position number is not to big.
    Try the movement at example vector 0, 0, 50000
    The cube only movement with lag.
    Set i the same cube, with the same movement script to 0, 0, 0 the movement is not more laggy.
    I don´t find a way to fix it. When i set a gameobject, first at 100001 coming a info it is to large.
    Why is then 50.000 to big?
    I don´t talk with a terrain, its a empty scene, only a cube and a movement script.
    It is not working from -100000 to 100000 ?

    In my test, little lag begin at 20000~
    I try vector forward, without x and y change.
    If the number to large for unity??

    ps. i have test it in Unity 5.6.3f1 and 2017.3.1f1.
    Both version have the same problem.


    Update 27.07.2018:
    Have create a new therad for this ( https://forum.unity.com/threads/movement-lag-when-position-over-20-000.542535/ )
     
    Last edited: Jul 27, 2018
    xeonheart likes this.
  6. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Thank you all for your input, so i went ahead with the first using the Vector3, though i did finally get it to work with GetKeyUP and GetKeyDown.Keycode etc., which was a great practice, and changed the logic since i didnt need the FixedUpdate(), Thank you Doug_B, and finally got it to work,
     
    Doug_B likes this.
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You will want to test the scenario where the user wants to move diagonal by pressing and holding W and A at the same time. The Vector3 approach should handle this.
     
  8. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hey Jeff,

    thanks for the input and test, yes i tested that, W/A, and W/D and works perfect :), the only weird thing is, my character cant move into the town, there is a fence around it, with an opening and houses inside the gate, but cant seem to get my character to move in there, any idea if you ran into a similar issue before? i can attach the project if needed so you can see?