- Home /
Add force in direction of collision
i am trying to make a pool/billiards game and am trying to add a force in the opposite direction of a collision. this is what ive come up with but it does not work. please help me out using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Text; using UnityEngine.EventSystems;
public class Cue : MonoBehaviour
{
public Rigidbody CueRB;
// // Start is called before the first frame update
void Start()
{
}
// // Update is called once per frame
void Update()
{
if(Rigidbody.OnCollisionEnter(Collision))
{
CueRB.AddForce(Collision * Time.deltaTime, 0, Collision * Time.deltaTime);
}
}
}
Answer by Captain_Pineapple · Mar 22 at 08:32 AM
Please search for some Collision Code examples and take a close look at how that is done properly. This just is not how you implement the callback "OnCollisionEnter".
What you need instead is something like this:
public void OnCollisionEnter(Collision col)
{
CueRB.AddForce(col.GetContact(0).normal);
}
Please also try to understand how function arguments work. You always need a type (Collision) followed by a name (col). Just writing "Collision" will not do the trick.
In addition to that please try to read into Time.deltaTime usage. If you have a continuous force or movement - multiply with Time.deltaTime. If you have single instances of force being applied then do not multiply with Time.deltatime.
Your answer
