Monster Move to Player.

Hello everyone. Kinda new to all this unity scripting and engine stuff… but i have managed to get my character moving around click style like in alot of action /rpg games. What i am trying to accomplish now, is to create a gameObject that has a secondary game object as its child acting as a trigger for detecting players entering the trigger. Like a vision trigger.

When the player enters the trigger, i want the gameObject (i will refer to it as the monster from now on) towards the player.
My problem is that all the Move, SimpleMove functions seem to evolve around the character controller, and this isnt a player / character at all. I looked into the rigidbody itself that is attached to the monster, and so far all i have been able to do is have the monster instantly move to the players location. I am really not sure even where to begin trying to understand how or what i need to do to move the Monster towards the player.

Heres what i have, but its sooo wrong its not funny :(. And it doesnt even come close to the desired efffect.

var speed : Vector3 = Vector3(1, 1, 1);
var CharacterDetected : boolean = false;

function OnTriggerEnter(object : Collider)
{
	if(object.transform.name == "Player")
	CharacterDetected = true;
}

function OnTriggerExit(object : Collider)
{
	if(object.transform.name == "Player")
	CharacterDetected = false;
}

function FixedUpdate()
{
	if(CharacterDetected)
		{
		var x : float = gameObject.Find("Player").transform.position.x + 1;
		var y : float = gameObject.Find("Player").transform.position.y;
		var z : float = gameObject.Find("Player").transform.position.z + 1;
		speed = Vector3(x, y, z);
		transform.parent.transform.rigidbody.MovePosition(gameObject.Find("Player").transform.position + speed * Time.deltaTime);
	}
}

If anyone could be so kinda to point me in the direction of even an article that may explain how to do this, i would be very greatful. Thanks in advance.

A simple way to do it would be to use transform.LookAt(): http://unity3d.com/support/documentation/ScriptReference/
and then simply move forward.

You could add a turning algorithm so he doesn’t instantly turn to face the player. Depending on your requirements, you will also have to devise a way to only rotate on a certain axis.

Try this out:

var speed : int = 2;
var CharacterDetected : boolean = false;

function OnTriggerEnter(object : Collider)
{
	if(object.transform.name == "Player")
	CharacterDetected = true;
}

function OnTriggerExit(object : Collider)
{
	if(object.transform.name == "Player")
	CharacterDetected = false;
}

function Update()
{
	if(CharacterDetected)
		{
                var player = gameObject.Find("Player");
                var monster = transform.parent.gameObject;
		var moveVector = player.transform.position - monster.transform.position;
                moveVector.ClampMagnitude(speed * Time.deltaTime);
                monster.transform.Translate(moveVector);
	}
}

The monster should not use a rigidbody component unless you want to treat it like a physics object.

I replaced my code with yers from above, but its throwing an error about the arguement list for ClampMagnitude(Vector3, float) is not suitable cause the only arguement givin is the float. I tried setting the Players transform as the vector, that didnt work, and i tried setting the moveVector as the vector to be used, also, no good. What Vector would it be looking for?

Oops sorry, lazy coding. ClampMagnitude is a static function, and has to be called using ClampMagnitude(vector3, float) instead of vector3.ClampMagnitude(float)

Replacing it with ClampMagnitude(moveVector, speed * Time.deltaTime); should fix the error.

Nah , if i replace it like you mention… than i recieve this error instead.

Unknown identifier: ‘ClampMagnitude’.

I got it, thanks alot dood :stuck_out_tongue: It just needed the value added properly to the moveVector.

moveVector = moveVector.ClampMagnitude(moveVector, speed * Time.deltaTime);

Thanks again.

I do however have one other issue now… and that is the monster constantly falls through the terrain :frowning: . Not completely, just ever so slightly as it moves further and further it slips away under the terrain. Man im so nub!

Ugh sorry again. (This is what I get for posting and not checking)

Vector3.ClampMagnitude(moveVector, speed * Time.deltaTime);

Lol , no worries , you got me much further than I , thanks, if you see above i edited the post, cause the only issue i have now, is that the monster is slowly falling through the terrain. The further along the path it moves, it seems to be slipping under the terrain.

I know from my character Mouse click move script that it was a case of using trigger states to read grounded or not. Im wondering if i will neeed to do something similiar with the monster object.

var speed : int = 2;
var CharacterDetected : boolean = false;

function OnTriggerEnter(object : Collider)
{
	if(object.transform.name == "Player")
	CharacterDetected = true;
}

function OnTriggerExit(object : Collider)
{
	if(object.transform.name == "Player")
	CharacterDetected = false;
}

function Update()
{
	if(CharacterDetected)
		{
                var player = gameObject.Find("Player");
                var monster = transform.parent.gameObject;
		var moveVector = player.transform.position - monster.transform.position;
                Vector3.ClampMagnitude(moveVector, speed * Time.deltaTime);
                monster.rigidbody.velocity = moveVector;
	}
}[/CONE]
By manipulating the rigidbody, it should stop the monster from falling through the terrain. I think lol