Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

AddForce not pushing back my RigidBody2D

$
0
0
I am trying to add force in the opposite direction of my colliding object (in this case the player). I've scrolled through a dozen of answers but they seem to give me no results. AddForce simply refuses to move the object. (2D space) This is how I move my enemy which I want to push back on collision. To clarify the object is Dynamic, Mass is 1 and gravity scale is 0: public void Movement_Follow(GameObject player, float Speed) { Vector3 playerPosition = player.transform.position; transform.position = Vector3.MoveTowards(transform.position, playerPosition, Speed); } However, even with the moving commented out and simply spawning them at a certain position and then running into them still gives me nothing. And this is how I try to push it back when it bumps in the player (opposite direction): private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.name == "Player") { health.Damage(5); float force = 50; //rb is the components RigidBody2D initialized in Start() rb.AddForce(-collision.gameObject.GetComponent().velocity.normalized * force, ForceMode2D.Impulse); } } I considered that maybe I don't have enough force applied, but even values of 500 000 produce no effect. Different methods of adding force also give me no results.

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>