- Home /
How to make player stop a certain distance away from enemy?
Ok so i use this script here (http://wiki.unity3d.com/index.php/Click_To_Move_C) to move my player. It is a clicktomove one. Is there a way to edit it so that when he gets at a certain distance away from the enemy, he stops?(and then maybe start attacking). Thanks!
Answer by Lairinus · Mar 12, 2013 at 04:28 AM
if (unitIsMoving)
{
float yourDistance = Vector3.Distance (firstVector, secondVector);
if (yourDistance <= x)
{
KillStuff();
}
It checks the distance between two GameObject positions and returns a float. Or really any two Vector3s.
I haven't looked at that script, but the first line of my pseudocode is basically what would work. Just throw it in an update.
This is the script i use. I get so many errors unfortunately :( Thanks for posting something anyway.
using UnityEngine; using System.Collections;
public class moveOn$$anonymous$$ouseClick : $$anonymous$$onoBehaviour { private Transform myTransform; // this transform private Vector3 destinationPosition; // The destination Point private float destinationDistance; // The distance between myTransform and destinationPosition
public float moveSpeed; // The Speed the character will move
void Start () {
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position; // prevents myTransform reset
}
void Update () {
// keep track of the distance between this gameObject and destinationPosition
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
if(destinationDistance < .5f){ // To prevent shakin behavior when near destination
moveSpeed = 0;
}
else if(destinationDistance > .5f){ // To Reset Speed to default
moveSpeed = 3;
}
// $$anonymous$$oves the Player if the Left $$anonymous$$ouse Button was clicked
if (Input.Get$$anonymous$$ouseButtonDown(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
}
// $$anonymous$$oves the player if the mouse button is hold down
else if (Input.Get$$anonymous$$ouseButton(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
// myTransform.position = Vector3.$$anonymous$$oveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
// To prevent code from running if not needed
if(destinationDistance > .5f){
myTransform.position = Vector3.$$anonymous$$oveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
}
}