AI script walking between 2 points on map.

Howdy fellow Unity’ers. I got a question for you folks, wonder if any might have an idea why this script runs the first ‘if’ command ok but doesn’t apply the rotation?
The character that has the script attached to it just walks to the wallA point and stop and just shakes like mad. He’s supposed to reach wallA, turn around 180 degrees and walk the other way until he reaches wallB and rotates, rinse and repeat.

var wallA : GameObject;
var wallB : GameObject;
var target : Transform;

function FixedUpdate ()
{
	if ( transform.position.x > wallA.transform.position.x )
	{	
		transform.position.x = transform.position.x - 0.03;
			if ( transform.position.x == wallA.transform.position.x )
				transform.Rotate ( Vector3.up * 180.0, Space.Self );
	}
	
	else if ( transform.position.x < wallB.transform.position.x )
	{	
		transform.position.x = transform.position.x + 0.03;
			if ( transform.position.x == wallB.transform.position.x )
				transform.Rotate ( Vector3.up * 180.0, Space.Self );
	}
}

Thanks again.

I think you should do this :

transform.Rotate ( Vector3.up * -180.0, Space.Self );

Multiply by negative value should rotate the object .

Hi userno101, I tried adding the negative sign to the javascript rotate code and the character still does the first ‘if’ statement (reaches wallA x position) and then goes into spasms. I can see it’s trying to rotate but can’t, so it shakes in place.

Does the character have time to get far enough away before the “spin 180 degrees!” code comes into play again on the next update?

I’m kind of new to scripting myself, but wouldn’t raycasting be the better way to go? You can detect the wall from whatever distance you choose, then initiate the turn or use Slerp if you want to control the rate of turn.

My question when casting a Ray is this: is there any way to find the angle at which the Ray intersects with the object when it hits it? If there is, then you could simply rotate that same angle, just in the opposite direction.

Example, entity is walking at a 20-degree angle toward a wall. The entity detects the wall with the raytracing, and turns 20 degrees away from the wall.

I’m not sure if there’s a direct way, but you might want to look at raycast.normal. The scripting reference is very thin in this area and not much help unfortunately. It will give you the normal of the surface that’s been hit. You could then calculate the angle between the normal and your object. There are a few postings that address that problem in an indirect way. Hopefully someone with more expertise than me will jump into this discussion. :roll: If I find out more, I’ll post again. Will be interested to see the solution.

Sounds like you walk past the waypoint, and if you do the location will never equal to the waypoint.

I would suggest something like this instead:

if ( Mathf.Abs(transform.position.x - wallB.transform.position.x) <= 0.5 )

Change that 0.5 to whatever works best for you.

Thanks all for the replies. So I finally got the AI to walk from wallA to wallB, rotate and repeat. Only thing left is to figure out why the script causes the GameObject to rotate like mad before it moves off in the opposite direction. Looks like interference from the collision planes wallA and wallB. Am I right? Here’s the code so far if you folks want to use it:

var wallA : GameObject;
var wallB : GameObject;
var target : Transform;
var speed : float = 0.1;

function FixedUpdate ()
{
	var count = 0;
	var weight = Mathf.Cos ( Time.time * speed * 2 * Mathf.PI ) * 0.5 + 0.5;
	
	if ( count == 0 )
	{
		transform.position.x = wallB.transform.position.x * weight;
			if ( Mathf.Abs ( transform.position.x - wallB.transform.position.x ) <= 0.1 )
			{
				transform.Rotate ( Vector3.up, 180 );
			}
			count = count + 1;
	}
	
	if ( count == 1 )
	{
		transform.position.x += wallA.transform.position.x * ( 1 - weight );
			if ( Mathf.Abs ( transform.position.x - wallA.transform.position.x ) <= 0.1 )
			{
				transform.Rotate ( Vector3.up, 180 );
			}
			count = count - 1;		
	}			
}

Bump! Still trying to figure out the “object rotating like mad” before it sets off in the opposite direction issue. I tried using:

transform.Rotate ( Vector3.up * 180 * Time.smoothDeltaTime );

and it worked well except the object didn’t keep a perfect 180 degree turn. The object kept rotating at different angles. Anyone know a good way to smooth or slow down the rotation when collision with wallA or wallB occurs? Right now, the object collides and starts to rotate 180 degrees but it does like 20 rotations in high-speed mode before it settles on the final 180 degree turn and goes off in the opposite direction.

Thanks.

this may or may not be what your wanting, but you COULD try using transform.LookAt(wallA); not sure if thats what your wanting though, it will make it instantly snap, and not be smooth, but atleast it won’t be spinning like crazy :stuck_out_tongue: