I have a player that is moving forward with constant speed and I have attached a rigidbody to that object. So I want that when player hit with other collider then a force should be apply on player to move it in backward direction. here below is my script that is attached with my player. The print statement is executing so it mean that the Trigger function is working and rigidbody is attached to the player, but the addforce()
function is not working.
Please help me.
void OnTriggerEnter(Collider obj)
{
if (obj.gameObject.name == "enemy")
{
if(gameObject.GetComponent<Rigidbody>())
{
print("force add");
gameobject.getcomponent<RigidBody>().AddForce (-transform.forward * 10f * Time.deltaTime);
}
}
}