Alternative to Vector3.MoveTowards()?

Hello,

This is my first post here, I got started with the free version of Unity a few days ago and am really liking it so far!

I’m working to get some simple AI going for my game, and I’ve made some good progress where the AI follows its target if it can see the target - now, I’m not using waypoints or nodes for my AI at this point (something I’ll probably have to look into in the future), just plain transform.position changes. I’m running into a problem where my AI, which is using a CharacterController, doesn’t respect the normal collision detection provided by CharacterControllers - it passes through objects that don’t have Rigidbodies attached to them. I figure this is because I use Vector3.MoveTowards() for its movement. Here’s the function in question:

function FollowTarget()
{
	transform.LookAt(targetPosition);
	
	var range = Vector3.Distance(targetPosition.position, transform.position);
	
	//Move towards the target while we're out of range.
	if(range > targetRange)
	{		
		transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, speed * Time.deltaTime);
	}
}

I tried making this work with CharacterController.Move() so that it respects collision detection, but got strange/inaccurate results with the movement itself. Can anyone suggest an alternative method of doing this?

ive tried forces, like

Vector3 dir2target = (bc.position - transform.position);

// and even slowed it down with magnitude of closeness…
// Vector3 dir2target = (bc.position-transform.position).magnitude/10*(bc.position - transform.position);
rb.AddForce(dir2target, ForceMode.Force);

… but for some reason the object just orbits its target bc.position :\

You can use Vector3.Slerp Unity - Scripting API: Vector3.Slerp