- Home /
Roll-a-Ball enemy ball AI
Hi everyone, well i watched the "roll-a-ball" tutorial and I got a good result after all. Now I'm trying to add some "bad balls", but I'm getting some problems with the AI. I'm using navmesh to do it, but when the bad ball hits the player nothing happens, it keeps hitting the player but didn't pushes him.
This is the code of the enemy ball:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
Transform player;
NavMeshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
nav.SetDestination (player.position);
}
}
What I'm doing wrong ? :\
Answer by ExplodingCookie · Mar 28, 2015 at 12:40 PM
Non-rigidbodies do not apply forces to Rigidbodies. Try adding a Kinematic Rigidbody to the bad ball to get a pushing effect. The other option is to get a script with an OnCollisionEnter that applies force in the direction the bad ball is going upon contact.
Great that worked! To do it with scripts how can I detect the direction to aply to force to it ?
The simplest way is to force the bad ball to face toward the good ball by using
function Update () {
transform.LookAt(//Good Ball reference here);
}
Then you can use
transform.TransformDirection(Vector3.forward);
to get the direction of the force.
Then use
goodBall.GetComponent.<Rigidbody>().AddForce(forwardVector * theForce);
to apply the force.
The syntax for the GetComponent may not be right. I'm not used to the new system yet.
O$$anonymous$$, by doing this I have to disable the $$anonymous$$inematic in the bad ball RigidyBody ?
$$anonymous$$inematic prevents the bad ball from having the physics engine mess up the Nav behavior. Turning $$anonymous$$inematic off is not what you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Best Practices for Physics + AI 1 Answer
Realistic Helicopter AI Movements using physics 0 Answers
Hi guys, I need your opinion regarding physics simulations in unity. 2 Answers
2D 360 degress platformer example needed 0 Answers
Collision Detection Between Instances of Prefabs doesn't work 1 Answer