- Home /
Question by
florezsantiago · Jul 21, 2018 at 06:22 PM ·
velocityphysics2dgamecontroller
How can I stop the velocity of an object in movement when I touch it?
I´m not an expert on swipe controllers, so I decided to find a tutorial on internet. It also worked, but I need to change the player velocity to 0 when i touch it while is moving, I consider that needs a double conditional, but i don´t know how to use it. I hope that someone can help me with that problem.
there is the script example:
public class SwipeScript2 : MonoBehaviour {
Vector2 startPos, endPos, direction;
Rigidbody2D myRigidbody2D;
public float shootPower = 10f;
void Start()
{
myRigidbody2D = GetComponent<Rigidbody2D>();
}
void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
}
}
void OnMouseUp()
{
if (Input.GetMouseButtonUp(0))
{
endPos = Input.mousePosition;
direction =startPos - endPos;
myRigidbody2D.isKinematic = false;
myRigidbody2D.AddForce(-direction * shootPower);
}
else
{
myRigidbody2D.AddForce(new Vector2(0, 0));
}
}
}
Comment
tutorials are a type to learn new things. If you're using a tutorials code but don't understand it, maybe you should try to learn what and how it works ins$$anonymous$$d of building on top of it.
because the next question is surely just around the corner if you keep implementing what you do not understand.
Your answer
![](https://koobas.hobune.stream/wayback/20220612171220im_/https://answers.unity.com/themes/thub/images/avi.jpg)