Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make enemy GameObject move on collision

Discussion in 'Editor & General Support' started by codesushi, Aug 31, 2017.

  1. codesushi

    codesushi

    Joined:
    Sep 27, 2014
    Posts:
    22
    I am trying to make an enemy's game object go flying when it collides with the player. Kinematic is disabled on both the player and the enemy object.

    I have this code in the enemy's class:

    void OnCollisionEnter(Collision collision) {
    if (collision.gameObject.tag == "Player") {
    Vector3 dir = collision.contacts[0].point - transform.position;
    dir = -dir.normalized;
    GetComponent<Rigidbody>().AddForce(dir * 100);
    }
    }

    However, while the enemy object does detect the collision, I do not see it react with the force of the Player.

    Any ideas on what could be happening?
     
  2. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    hard to say :/ your code does look fine. One thing is to check the mass of your enemy, 100 force might seem like a lot but not if its mass is high.

    otherwise check for instances in your enemies code for things like rigidbody.Velocity = X since that will overwrite any force changes once its called.

    Also worth checking for things like transform.position = X