I have seen many scripts showing how the objects attack the the player? would like to ask whether how should i script when the player near the object, the object will walk away?
You can compute the direction in which to move as:
Vector3 direction = transform.position - player.transform.position;
direction.Normalize();
You can then use this direction vector however you want (for example, you could apply a force in the specified direction if the game object has a rigid body attached, or you could use the vector, scaled by time and speed, as input to the ‘simple move’ function of the character controller component).
if(distanceBetweenPlayer_and_AI < 10)
{
transform.LookAt(Player);
transform.Rotate(0, 180, 0);
transform.Translate(Vector3.forward);
}
How would i define my variable if it is script above in this format.
if(distanceBetweenPlayer_and_AI < 10)
{
transform.LookAt(Player);
transform.Rotate(0, 180, 0);
transform.Translate(Vector3.forward);
}
How would i define my variable if it is script above in this format.
What do you mean by ‘define my variable’?
If you’re asking how to determine the distance between two game objects, you can use Vector3.Distance().
kk thanks, btw i just implemnent the waypoint codes and keep getting the error of:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) System.Object:stelemref (object,intptr,object)
waypoint.Awake () (at Assets\waypoint.js:7)
var waypoint : Transform[ ];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
function Awake(){
waypoint[0] = transform;
}
function Update (){
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Any idea know why? do i need to amend my resolution, setting or size, etc to fit the range?
kk thanks, btw i just implemnent the waypoint codes and keep getting the error of:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) System.Object:stelemref (object,intptr,object)
waypoint.Awake () (at Assets\waypoint.js:7)
var waypoint : Transform[ ];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
function Awake(){
waypoint[0] = transform;
}
function Update (){
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Any idea know why? do i need to amend my resolution, setting or size, etc to fit the range?