- Home /
Problem getting players position.
So I've got my character moving around and using a raycast to pick up the player, which should then turn hostile and move toward the player to attack.
However the problem I'm having is that the player is Vector3 and because of this I can't drag it into the inspector, so as a default, when the character see's the player, it moves to the 0,0,0 coordinates.
So I'm assuming I need to convert the Vector3 or something. Please not just setting the player variable to a transform didn't help.
var sightDistance : float;
var waypoint : Transform[];
private var currentWaypoint : int = 0;
var player : Vector3;
var bait : Transform;
var state : String ;
var target : Transform;
var walkSpeed : float;
var loop : boolean = true;
private var character : CharacterController;
function Awake()
{
character = GetComponent(CharacterController);
state = "natural";
}
function Update ()
{
print (target);
var dir = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var attDirection : Vector3 = player - transform.position;
if (Physics.Raycast(transform.position, dir, hit, sightDistance))
{
// If we see Bait.
if (hit.collider.gameObject.tag == "Bait")
{
state = "baited";
}
// If we see the Player.
else if (hit.collider.gameObject.tag == "Player")
{
state = "hostile";
}
else if (hit.collider.gameObject.tag == "Cover")
{
}
else
{
state = "natural";
loop = true;
}
}
if (state == "hostile")
{
loop = false;
target = player;
character.Move(attDirection.normalized * walkSpeed * Time.deltaTime);
}
else if (state == "natural")
{
if (currentWaypoint < waypoint.length)
{
if(moveDirection.magnitude < 1)
{
transform.position = target;
currentWaypoint = (currentWaypoint + 1)% waypoint.length;
}
else
{
//transform.LookAt(target);
character.Move(moveDirection.normalized * walkSpeed * Time.deltaTime);
}
}
else
{
if(loop == true)
{
currentWaypoint = 0;
}
}
}
}
Is there any reason you can't make player
a GameObject
and use player.transform.position
ins$$anonymous$$d?
You can make the player a GameObject as @jbarba_ballytech suggests or a Transform. Both will work. With a transform, you would replace your references to 'player with' to 'player.position'.