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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rigidbody decreasing speed after collission

Discussion in 'Scripting' started by busterlock, Apr 15, 2018.

  1. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    I'm creating an endless runner (in 2D) in which there is a force directed only to the right, multiplied by a Vector3 (1,0,0) that sets his movement speed.
    The code I have is the following:
    Code (CSharp):
    1. public float speed;
    2. public Rigidbody rb;
    3.  
    4. void Start()
    5.     {
    6.         rb.GetComponent<Rigidbody>();
    7.         direction = new Vector3(1, 0, 0);
    8.     }
    9.  
    10. void FixedUpdate()
    11.     {
    12.         transform.Translate(direction * speed);
    13.     }
    The code works and the player moves, everything is fine. My problem is that if he hits a platform (horizontally), his speed decreases to a certain value, and then it remains constant on that value.
    The easy way to fix this would be to increase the distance between platforms, so I have a way to fix it, I was wondering if there is another way, and came up here to get help.

    Thanks!
     
  2. ravirajPlaymotion

    ravirajPlaymotion

    Joined:
    Jan 28, 2017
    Posts:
    15
    Hello busterlock

    Few questions first :
    Do u want to keep the speed as constant even after hitting the platform ?
    By any chance are u reducing the speed when the character collides to the platform?

    Thanks.
     
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    Exactly, I want to keep the speed constant! And regarding the speed decrease when the collision happens, I am not doing it manually, but because of the forces operations, the character's speed decreases by itself.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This isn't using forces or the physics system at all. You're simply moving the transform which is probably becoming stuck inside a collider while you endlessly teleport it. Set the velocity of the rigidbody instead.
     
    zakdank likes this.
  5. ravirajPlaymotion

    ravirajPlaymotion

    Joined:
    Jan 28, 2017
    Posts:
    15
    Yes the speed will decrease as you are using Rigid bodies and applying force to it, any rigid body moved using force will slowdown after collision.

    you need to set the velocity as mentioned by "GroZZleR".