bouncy collision not working! Thank you
I have this 2D topdown game were my player moves by mouse click. I attach a script to my player that If my player collides with an moving object, my player should bounce off. Which works, but not how I wanted it to. Once my player collides with the object my player bounces up in the velocity of (as an example): X: -2.079025 Y: 136.8411 Z: 10.14. And continues moving. Disabling my player movement script. Which I do not want. What I want is for my player to bounce of the moving object (disabling the player movement) left, right, up or down depending on where moving objects are facing, but only for a short amount of distance. Then re-enabling the player movement script as normal. How would I do that? Thank you!
Here's my bouncy script:
void OnCollisionEnter2D(Collision2D Col)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0); // set y velocity to zero
GetComponent<Rigidbody2D>().AddForce(new Vector2(0,1000)); // some constant force here
}
And here's my player movement script:
public float speed = 15f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
[Help] Player stops moving when hitting wall diagonally 0 Answers
Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers
How to move the player only 1 tile per buttonPress? 2 Answers
Navmesh based player movement 1 Answer
How do I add animation to my script? 1 Answer