1
votes

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); 
        }
    } 
}
1

1 Answers

1
votes

Ok, first try apply higher value, it's possible that force is too low, bacause it's multiply by Time.deltaTime which is low value:

.AddForce (-transform.forward * 10000f * Time.deltaTime);

Second check, if you change position of the player with transform.position anywhere in your code. (If yes paste this parts of code)