shipAI

Can anyone help me with my shipAI script? My enemy ship will not move. This is strange though, I am getting an error message, An instance of type UnityEngine.Collision is requed to access non static member gameObject.

//var health = 100.0;
var isViewing = false;
//var slashAttack = 15.0;
//var biteAttack = 25.0l
var speed = 20;
var rotateSpeed = 10;
var leftRight = 3;
var inFront = 7;
var direction = 1;
var player = GameObject.FindWithTag ("Player");

function OnTriggerEnter(other: Collider)
{
	if (Collision.gameObject.player)
	{
		isViewing = true;
	}
	
	if (isViewing)
	{
		transform.LookAt(player.transform);
		transform.Translate(Vector3.forward * Time.deltaTime);
	}

}

	function Update()
	
	{
		if (!Physics.Raycast(transform.position, transform.forward, inFront))
		{
			transform.Translate(Vector3.forward * speed * Time.deltaTime);
		}
		
		else
		{
			if(Physics.Raycast(transform.position, -transform.right, leftRight))
			{
				direction = -1;
			}
			
			else if (Physics.Raycast(transform.position, transform.right, leftRight))
			{
				direction = 1;
			}
		}
	}
	
	transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime * direction
function OnTriggerEnter(other: [B]Collider[/B])
{
	if ([B]Collision[/B].gameObject.player)

You can’t see the bolded part really well, but it’s there. Just squint a little. =P

Do I have to take the bolded part out? I want to use the OnTriggerEvent so when the enemy see my ship it will attack me

Well… from looking at your script… your error lies in this line:

if (Collision.gameObject.player)

to fix this, you will need to compare the game object of the collision to the player (which is a game object)

if (other.gameObject==player)

As far as your movement…

You are basically moving the “ship” directly towards the player. This will work once the error is fixed.

Now… Your update is really not working here at all. Basically, the first line says… If something is within inFront units in front of me, move forward… if not, check to see if something is on either side and rotate in that direction… until… something is in front of me again.

The whole thing is convoluted though. If an object is right or left of something for 1 frame, it rotates slightly then stops. If nothing is ever in front of the unit, it will not move.

Lastly, the actual script appears to be designed for a basic monster, not a space ship. In a space ship, you would want to use physics for movement. So if the object is left. turn the object to face left only slightly this frame, and continue to turn it slightly until it gets to the point where it is facing towards the player. Thrust can then be determined by angle and distance. Thus giving you a proper moving AI.

Thanks BigMasterB. I will get on it.