- Home /
Push depending on speed
I'm making a game where the player controls an object and has to push other objects, but I'm having trouble making the object being pushed depend on the player objects speed.
This is what I'm using now.
void OnCollisionEnter(Collision c) { if (c.gameObject.tag == "Pushable") { Vector3 dir = c.contacts[0].point - transform.position; float force = 2000; c.gameObject.GetComponent ().AddForce(dir * force); } }
The problem is it pushes with the same force no matter how fast the players object is moving.
Answer by Arterion · May 19, 2018 at 12:55 PM
It pushes the object with the same force, because you didn't referenced the speed of the player anywhere. You just use a base force (2000f) and the local Vector to the pushable object (?).
If your are using a Rigidbody you can simply get the speed with Rigidbody.Velocity or you could use the Input of the player as a speed reference.
If you still don't see any changes try to increase the base value.
Your answer
Follow this Question
Related Questions
How to change CC script to Rigidbody script 1 Answer
Disable/Enable Colliders 1 Answer
CapsuleCast colliding improperly 2 Answers
Gun collision problem 3 Answers
How do you enable the layer based collision matrix? 1 Answer