Hello,
I’m a new user to Unity and still learning to script, and I’m having trouble with getting a character to “seek” a target object, that is, getting it to move by itself until it has reached an object instead of having the user control the character using arrow keys. The first thing I would like to be able to do is have the character move from its start location to a box after clicking a GUI button. All I can figure out so far, though, is how to make the character move using input from the arrow keys. I’m trying to write a seek function that activates at the button press, with the most simple version being where the character starts already facing the object and only has to move forward until it reaches it. Something like this that gets called in Update(), but I have no idea how to make the movement happen once the button is pressed once:
function seek(){
//start with the character facing the target
transform.LookAt(target);
//distance from character to target
var distanceBetween = Vector3.Distance(this.transform.position, target.transform.position);
while (distanceBetween !=0){
//move the character until the object is reached
}
}
I’m pretty new to this, so any help is much appreciated. Thanks
I’m trying to apply this seek function to my player http://www.andrewtlee.net/blog/?p=6 but having trouble implementing it (I’m very new at this). I’ve been given an environment with a character and a cube, where the cube is supposed to be the target. However, I have no idea how to refer to that cube as the target within my code. My character is simply supposed to walk automatically to the cube and then stop when it reaches it. If anyone has any advice on how to get it to work that would be great… here is the relevant part of what I have in player.js at the moment
// Cache a reference to the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);
var target : Transform;
var speed = 6.0;
var rotateSpeed = 120.0;
var desiredDistance : float = 1; //distance to stop from target
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
//Seek(targetPos); // how do I get targetPos?
//code to move the character with arrow keys
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Mathf.Abs(Input.GetAxis("Horizontal"))>0 || Mathf.Abs(Input.GetAxis("Vertical"))>0){
characterController.Move(moveDirection * Time.deltaTime);
animation.Play("walk1");
}
else {
animation.Play("idle1");
}
}
function Idle() {
return 1;
}
function Seek(targetPos){
//set the object target to variable passed in function
var destination : Vector3 = targetPos.position;
var distanceToTarget : Vector3 = destination - transform.postion;
var desiredVelocity : Vector3 = distanceToTarget.normalized * speed;
if (distanceToTarget.magnitude < desiredDistance){
rigidbody.velocity = Vector3.zero;
}
else{
rigidbody.velocity = desiredVelocity;
}
}
If you are unsure how to reference a cube in your script, you have a long way to go.
Let me just say, in general, one declares vars. a var must be a type. A type in your case for cube is a GameObject. Once you declare that var, you can reference it.
Try…
var cube : GameObject;
function Start()
{
print(cube.transform.position);
}
See what you get.