Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Th3D1g1talJ3di · Aug 18, 2012 at 08:30 AM · javascriptclickon

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ScroodgeM · Aug 18, 2012 at 08:45 PM

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; }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Th3D1g1talJ3di · Aug 19, 2012 at 02:30 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

When I click, my enemy doesnt lose health... 0 Answers

JavaScript Version of foreach (Touch touch in Input.touches) 1 Answer

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers

How to make a selection system? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges