- Home /
Need help with script to find and persue target on click.
I need help (YOU DON'T SAY!!). I am trying to make a script that makes the player character go after a target that has been clicked on. Right now I have the script that identifies if the target clicked on is an enemy or not using ray casts and tags, but I need help on how to make the character chase that target and stop within close proximity of it and also have it stop if the player clicks somewhere else. Here is my code so far:
#pragma strict
private var AttackTarget : Transform;
var speed = 10.0;
function Update () {
if ( Input.GetMouseButtonUp(0)){
var hit : RaycastHit;
var rayEnemy = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(rayEnemy, hit)){
if(hit.collider.transform.CompareTag("Enemy")){
AttackTarget = hit.collider.transform;
BarbarianBasicAttack();
}
}
}
}
function BarbarianBasicAttack () {
}
As you can see I am hoping to put the code under BarbarianBasicAttack. Idk if this affects the way it is coded but here is the movement script as well:
#pragma strict
var speed = 4.0;
var TurnSpeed = 4.0;
function Update () {
if(Input.GetKey(KeyCode.Mouse0)){
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, TurnSpeed * Time.deltaTime);
transform.position += transform.forward * speed * Time.deltaTime;
}
}
}
I was thinking of using a ray cast towards the target and stopping the character when the hit is true, but I am still a beginner in programming.
Please put answers in Javascript pls. Thanks.
Answer by ScroodgeM · Aug 18, 2012 at 08:45 PM
EnemyTracker.js
#pragma strictprivate var AttackTarget : Transform;
var speed = 10.0;
function Update () { if ( Input.GetMouseButtonUp(0)){ var hit : RaycastHit; var rayEnemy = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast(rayEnemy, hit)){ if(hit.collider.transform.CompareTag("Enemy")){ AttackTarget = hit.collider.transform; BarbarianBasicAttack(); } } }
}
function BarbarianBasicAttack () { GetComponent(PlayerTracker).AttackTarget = AttackTarget; }
PlayerTracker.js
#pragma strictvar AttackTarget : Transform;
var speed = 4.0;
var TurnSpeed = 4.0;
var ToEnemyMinDistance = 1.0;
function Update () { if(Input.GetKey(KeyCode.Mouse0)){ var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0; if (playerPlane.Raycast (ray, hitdist)) { var targetPoint = ray.GetPoint(hitdist); MoveTo(targetPoint); } AttackTarget = null; } else if (AttackTarget) { if ((AttackTarget.position - transform.position).sqrMagnitude > ToEnemyMinDistance * ToEnemyMinDistance) { MoveTo(AttackTarget.position); } } }
function MoveTo(moveTo : Vector3) { var targetRotation = Quaternion.LookRotation(moveTo - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, TurnSpeed Time.deltaTime); transform.position += transform.forward speed * Time.deltaTime; }
Thanks. Would of taken me days to figure out how to do that, works like a charm.