- Home /
Other
Knockback player
Hi !
I tried to knockback the player when he collide with an ennemy but i'ts not really a knockback but a teleportation backward. Here is the code :
[SerializeField]
private float force;
void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "Player")
{
collision.transform.GetComponent<CharacterController>().Move(this.transform.forward * force);
GameController.removeLife(1);
}
}
How can I do ? Thank you !
Yo, did that scout just run up and miss both force of nature shots?
Answer by Smsaliou5935 · Nov 17, 2020 at 11:38 PM
Do public Rigidbody rb, then do void OnCollisionEnter(Collision collision) { if (collisionInfo.collider.tag == "Enemy") { rb.AddForce(Vector3.back forwardForce Time.deltaTime, ForceMode.VelocityChange); GameController.removeLife(1); } }
sorry for not posting in on code lines.. $$anonymous$$y bad, this was my very first time answering
Answer by ypermat · Nov 18, 2020 at 04:18 PM
No problem, where do I need to put theses lines ? instead of mine ? Because if I try to type only your lines like this : public Rigidbody rb;
void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "Player")
{
rb.AddForce(Vector3.back forwardForce Time.deltaTime, ForceMode.VelocityChange);
GameController.removeLife(1);
}
}
VS studio tells me that "forwardForce" and "Time" does not exist. He expected a ",".
PS: The script is on my ennemies, should I make one for the player instead ?
rb.AddForce(Vector3.back * forwardForce * Time.deltaTime, Force$$anonymous$$ode.VelocityChange);
You need to multiply those values.