- Home /
How to make an NPC ball to chase player by rolling, not moving
Hi,
I've built a Navigation Mesh and created a NavMesh Agent for an NPC sphere GameObject. In order for the NPC to chase the player, I've created this script for the sphere object :
using UnityEngine;
using System.Collections;
public class MoveTo : MonoBehaviour {
public float speed = 5.0F;
GameObject player;
Rigidbody rb;
Vector3 movement;
NavMeshAgent ball;
void Start () {
player = GameObject.Find ("Player");
rb = GetComponent<Rigidbody> ();
ball = GetComponent<NavMeshAgent>();
}
void FixedUpdate() {
Pursuit ();
}
void Pursuit() {
ball.destination = player.transform.position;
transform.LookAt (ball.destination);
rb.AddRelativeForce (Vector3.forward.normalized * speed);
}
}
The ball chases the player, but the problem is that the ball is moving without rolling. I've tried to use Rigidbody.MovePosition, but the ball simply teleports to my player (no matter if I enable or disable interpolate in the Inspector for the RigidBody):
void Pursuit() {
rb.MovePosition(transform.position + transform.forward * Time.deltaTime);
}
I would appreciate any hint on how to implement an automatic navigation rolling movement for the ball.
Thanks a lot
Your answer
Follow this Question
Related Questions
MovePosition not as precise as Translate with Kinematic rigidbodies (example unitypackage included) 1 Answer
Inconsistency in Rigidbody.MovePosition 0 Answers
NavMesh Collision Detection? 0 Answers
Adding a force to an object relative to a rotation? 0 Answers
Move a rigidbody when Kinematic ?? 3 Answers