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

Slowdown while moving with rigidBody2D movement

Discussion in '2D' started by JuiceBox, Feb 2, 2015.

  1. JuiceBox

    JuiceBox

    Joined:
    Jan 24, 2015
    Posts:
    41
    Hello! This is my first post on the forum so sorry if this is in the wrong place. ^)^ I've been having some trouble with rigidbody2d movement where the player slows down sometimes while moving. I have a very basic movement script, so this is probably an easy thing to fix. I'm not sure but it may be lag, but I really doubt that. So here's the movement script for my player
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.    
    6.     public float speed;
    7.  
    8.     void Update () {
    9.  
    10.         //Movement from Keyboard Input
    11.         if (Input.GetKey (KeyCode.D)) {
    12.             rigidbody2D.AddForce(Vector2.right * speed);
    13.                 }
    14.         if (Input.GetKey (KeyCode.W)) {
    15.             rigidbody2D.AddForce(Vector2.up * speed);
    16.         }
    17.         if (Input.GetKey (KeyCode.S)) {
    18.             rigidbody2D.AddForce(-Vector2.up * speed);
    19.         }
    20.         if (Input.GetKey (KeyCode.A)) {
    21.             rigidbody2D.AddForce(-Vector2.right * speed);
    22.         }
    23.  
    24.     }
    25. }
    26.  
    His speed variable is set to 4.5 right now.
    Here's a screenshot of his rigidbody properties also,

    Also his sprite properties (it has a 64x64 pixel sprite)

    Thanks in advance! ^ -^
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try rigidbody2D.velocity

    and set Rigidbody2D Interpolate to interpolate, for more smooth move.
     
  3. JuiceBox

    JuiceBox

    Joined:
    Jan 24, 2015
    Posts:
    41
    Alright I'll try that, thanks!
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    and i think using GetAxis is more easy. Like:

    float moveX = Input.GetAxis("Horizontal");
    rigidbody2D.velocity = new Vector2(moveX * speed, rigidbody2D.velocity.y);
    float moveY = Input.GetAxis("Vertical");
    rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, moveY * speed);

    you can adjust axis in project setings - input

    for physic changes is better to use FixedUpdate instead Update.
     
  5. JuiceBox

    JuiceBox

    Joined:
    Jan 24, 2015
    Posts:
    41
    right now i decided to replace the rigidbody2d.addforce with rigidbody2d.velocity =, but i'll go ahead and try that ^)^
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    i have changed script, it was one error there
     
    JuiceBox likes this.
  7. JuiceBox

    JuiceBox

    Joined:
    Jan 24, 2015
    Posts:
    41
    I fixed it and now it works perfectly! Thank you so much! :D

    [Fixed Script]
    Code (CSharp):
    1.  void FixedUpdate()
    2.     {
    3.  
    4.         float moveX = Input.GetAxis("Horizontal");
    5.         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, moveX * speed);
    6.         float moveY = Input.GetAxis("Vertical");
    7.         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.y, moveY * speed);
    8.         }
    9.     }
    10.  
     
    theANMATOR2b likes this.
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153