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

Stop Momentum after Respawn

Discussion in 'Scripting' started by devon_mathis, Oct 4, 2015.

  1. devon_mathis

    devon_mathis

    Joined:
    Feb 1, 2015
    Posts:
    19
    Hello all. I'm having a hard time figuring this out. I've searched all over and I cannot find a solution to help me fix the problem.

    I have a 2D platformer. I have a checkpoint system. I have a kill floor. When the player reaches a checkpoint, it saves that position. When the player hits a kill floor, it sends them to the checkpoint position. This script is attached to my player controller. It checks if it runs into a game object whose tag is "killZone".

    My problem is this: When my character falls from a height, it retains its momentum and when it goes to the checkpoint location it flies into the air.

    I don't want this to happen. When my character "respawns" at a checkpoint, I want him to be perfectly still. I tried using velocity = Vector3.zero; but I can't seem to get it to work. Here's my code.

    using UnityEngine;
    using System.Collections;

    public class KillZone : MonoBehaviour
    {
    Transform SP;
    Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody> ();
    SP = GameObject.FindGameObjectWithTag("SpawnPoint").transform;
    }

    public void OnTriggerEnter (Collider col)
    {
    if(col.gameObject.tag == "killZone")
    {
    transform.position = SP.position;
    rb.velocity = Vector3.zero;


    }

    }
    }
     
  2. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    I tried replicating your scenario but couldn't reproduce the problem you're describing. Setting transform.position and zeroing out the rigidbody's velocity should work.
    Are you sure there isn't some other force that could be getting applied on the player object? Also, you mentioned you were building a 2D platformer but are referencing Rigidbody instead of Rigidbody2D - is that correct?
     
  3. devon_mathis

    devon_mathis

    Joined:
    Feb 1, 2015
    Posts:
    19
    That is correct, about the Rigidbody. Is there a major difference between the two?

    What happens is exactly is this:

    There is a pit the player has to jump over. If he falls into the pit (by not making the jump) he moves to a checkpoint location. But the momentum from falling stays with him, and he flys up every single time. IF you move in a "x" direction while falling into the pit, the player does not shoot up. But if the player falls STRAIGHT down, then he flies up upon moving to the checkpoint location.
     
  4. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    Yes, Rigidbody is for 3D physics simluation, and Rigidbody2D for, well, 2D physics. You can still build what's essentially a 2D platformer with 3D physics components, as long as you don't use that extra dimension - I just thought it was odd you were using the 3D component instead of it's corresponding 2D one. I'm assuming all your other physics components correspond to their 3D variations as well.

    I'm also a bit suspicious that you mention the player shoots up after falling down into the killzone, and the fact that this doesn't happen while moving along the x-axis makes me think that you must have something else in play that's setting/modifying the rigidbody velocity without your knowing. Maybe that rings a bell? Otherwise I would start by testing what you're expectations are against what you're actually seeing: throw some debug statements or a pause directly after hitting the trigger and see what does/doesn't check out.

    Or if your at your wits end and are able to upload your project (or a minimal version of it that replicates the issue) I wouldn't mind taking a gander at it meself and see if I can determine what the problem is.
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you tried setting the velocity to zero before you change the position? Can you include the player movement script as well so we can see how he is being moved & jumped. Also, please use code tags as it is easier to read.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it's falling down, respawns and flies up? that's not maintaining momentum/velocity, that sounds a lot more "spawned too close to another collider"
     
    Kiwasi and devon_mathis like this.
  7. devon_mathis

    devon_mathis

    Joined:
    Feb 1, 2015
    Posts:
    19
    Oh my God. You're a freaking genius. This ^^^

    I didn't even know that was a "thing" >.<

    Thank you so much! Can you explain to me as to why that happens when too close to another collider?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sure. The physics engine deals with collisions by adding a force to colliders that overlap to move them apart. Normally this isn't an issue. But if colliders overlap a significant amount then the force applied will be significant enough to force them apart with significant speed.
     
    Nigey likes this.
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    *mumbles something about the starburst manoeuvre... *
     
    Nigey likes this.