x


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.

more ▼

asked Aug 18 '12 at 08:30 AM

Th3D1g1talJ3di gravatar image

Th3D1g1talJ3di
3 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

EnemyTracker.js

#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 () {
    GetComponent(PlayerTracker).AttackTarget = AttackTarget;
}

PlayerTracker.js

#pragma strict

var 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;
}
more ▼

answered Aug 18 '12 at 08:45 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

Thanks. Would of taken me days to figure out how to do that, works like a charm.

Aug 19 '12 at 02:30 AM Th3D1g1talJ3di
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3461
x1954
x225
x72

asked: Aug 18 '12 at 08:30 AM

Seen: 230 times

Last Updated: Aug 19 '12 at 02:30 AM