stop Addforce
Okay, I have a player that bounces off a tagged object on trigger, using Addforce. the force is set to *300, causing my player to get knocked back, which continuously move . So I placed a cube with a 2D box collider attached to it to stop the player from moving too far. My question is: how can I implement the Addforce to only move a short amount of distance, OR have a timer that stops Addforce after the trigger event occurred. Can someone help me with this. Thank you! this is my script:
using UnityEngine;
using System.Collections;
public class Moveplayer : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().AddForce(transform.right * 300);
GetComponent<Rigidbody2D>().AddForce(transform.right * 300);
}
}
Answer by joemane22 · Nov 14, 2015 at 05:18 AM
You could use
GetComponent<RigidBody2D>().AddForce(transform.right * 300, ForceMode.Impulse);
This will add a non-continuous force to the object which will stop moving as long as there is some drag set on the rigidbody. It will also be a little weaker so you may have to tweak the size of the force.
Also as a side note if you are using Rigidbody2D a lot in that script you might just want to create a field for it and assigning it inside that Start() method that way you don't have to get the component constantly which will give you a performance hit in the long run. Of course, if it is just for that one trigger event it should be all good.
Thank you for replaying beck , It works perfectly! I just have one problem. Why is it my player is being dragged back after collision. (kinda like it's being pulled back by a magnet). How would I stop this. All I want is for my player to get pushed back for a short amount of distance and stays there! Thank you again
Add linear drag, angular drag is for rotation. You will have to tweak the numbers to get to what works for your game. To make it easier add a public variable to your class called $$anonymous$$nockBackForce and replace it like this
GetComponent<RigidBody2D>().AddForce(transform.right * $$anonymous$$nockBackForce, Force$$anonymous$$ode.Impulse);
so you can tweak it from the inspector.
If the drag is high enough and you add the right amount of force along with it, it will only be pushed back a bit. Now there is a different way you can go about it by hard coding the knock back but I think if you play around with the numbers a bit it should work just fine.
Also, I explained Force$$anonymous$$ode.Impulse wrong, it's more of like getting hit rather than a smooth force, thought it might suit your needs a little better.
sorry I just fixed it then re-edit my comment, I'll just post it again.
Thank you for replaying beck , It works perfectly! I just have one problem. Why is it my player is being dragged back after collision. (kinda like it's being pulled back by a magnet). How would I stop this. All I want is for my player to get pushed back for a short amount of distance and stays there! Thank you again
Alright guess hard coding it is the way to go I made a simple script that will do what you need
public class $$anonymous$$nockBack: $$anonymous$$onoBehaviour
{
//The total distance the player will be knocked back
public float $$anonymous$$nockBackDistance = 10;
//the speed at which the player will be moving
public float $$anonymous$$nockBackSpeed = 1;
//control variable to flag this script to begin running
private bool running = false;
//keeps track of the time elapsed since it started
private float elapsedTime = 0;
void Start()
{
//nothing needed in here
}
void Update()
{
//if the running boolean was flag begin the knock back
if(running)
{
//add to the transforms position //this bit calculates the velocity it has to go to reach the distance in the right time
transform.position += transform.right * ($$anonymous$$nockBackDistance / $$anonymous$$nockBackSpeed) * Time.deltaTime;
//add the change in time to the elapsed time
elapsedTime += Time.deltaTime;
if(elapsedTime >= $$anonymous$$nockBackSpeed)
{
//flag the script to stop running and reset the elapsed time to be ready for the next time
running = false;
elapsedTime = 0;
}
}
}
void OnTriggerEnter2D(Collider2D obj)
{
//if the object collided with is the desired one flag for the script to start
if(obj.tag == "Bouncey object")
{
running = true;
}
}
}
please do read through the code comments till you fully understand what's happening. It has a few much-needed concepts in the code you will need to know. If you have any questions on why something works or if there is a bug(altogether possible but I don't think so) feel free to ask.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Force and Velocity problem 0 Answers
Spawned objects won't stop moving after instantiating. 2D Top-Down 1 Answer
Fall down Cube object from platform. 0 Answers
How to move an Instantiated Object towards a moving Object? 0 Answers
Enemy AI Script Error 1 Answer