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. Dismiss Notice

My character moves extremely slow

Discussion in 'Scripting' started by drakkwyr001, Jul 3, 2019.

  1. drakkwyr001

    drakkwyr001

    Joined:
    Jul 1, 2019
    Posts:
    15
    The jumping and the rotation works fine although the character moves at a extremely slow speed, I tried to tweak the values of my code (even with high values like 3000) and the mass but nothing happens…
    Heres the code:
    Code (CSharp):
    1. public class scr : MonoBehaviour
    2. {
    3.     //Variables
    4.  
    5.     public float speed = 30000;
    6.     private Rigidbody rb;
    7.     private float midJump = 1;
    8.  
    9.     //Character Movement
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.      
    15.     }
    16.     void FixedUpdate()
    17.     {
    18.  
    19.  
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.         float moveVertical = Input.GetAxis("Vertical");
    22.  
    23.      
    24.         Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    25.         rb.transform.position += movement * Time.deltaTime * speed;
    26.  
    27.      
    28.  
    29.         //Character Jump
    30.  
    31.  
    32.         if (midJump == 1 && Input.GetKeyDown(KeyCode.Space))
    33.         {
    34.             rb.velocity = new Vector3(0, 45, 0);
    35.             midJump = 2;
    36.  
    37.         }
    38.         else if (rb.velocity.y == 0)
    39.             midJump = 1;
    40.      
    41.  
    42.         //Character Rotation
    43.  
    44.         {
    45.             if (Input.GetKeyDown(KeyCode.D))
    46.                 rb.MoveRotation(Quaternion.Euler(0, 90, 0));
    47.  
    48.             if (Input.GetKeyDown(KeyCode.A))
    49.                 rb.MoveRotation(Quaternion.Euler(0, -90, 0));
    50.  
    51.             if (Input.GetKeyDown(KeyCode.W))
    52.                 rb.MoveRotation(Quaternion.Euler(0, 0, 0));
    53.  
    54.             if (Input.GetKeyDown(KeyCode.S))
    55.                 rb.MoveRotation(Quaternion.Euler(0, 180, 0));
    56.  
    57.  
    58.  
    59.  
    60.  
    61.         }
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.     }
    71. }
    72.  
     
  2. SmartMediaNL

    SmartMediaNL

    Joined:
    Sep 29, 2016
    Posts:
    77
    I think you need to use moveposition like you are doing the MoveRotation Link
     
    drakkwyr001 likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Just setting the transform position should work, not recomended on rigidbodies because of other rrasons, but should work.
     
    drakkwyr001 likes this.
  4. drakkwyr001

    drakkwyr001

    Joined:
    Jul 1, 2019
    Posts:
    15
    With MovePosition the rigid body doesnt even move and I cant jump :confused:
     
    Last edited: Jul 3, 2019
  5. drakkwyr001

    drakkwyr001

    Joined:
    Jul 1, 2019
    Posts:
    15
    I found the problem and it has a weird solution, since the speed variable is public I can change it on the inspector, but somehow the Speed on the inspector is 2, if I change it there the speed also does, but if I change in the code the speed is not affected :/ Anyways now its solved thanks alot for the help guys!
     
    SmartMediaNL and SparrowGS like this.
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    Keep in mind that for any variables that you show in the inspector- whatever value you type in the inspector will always precede whatever you initialize the value to in your code.