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
2
Question by Persona · Oct 28, 2010 at 02:26 PM · raycastlookattargetlockon

Using Raycast to "Lock On"

I'm trying to use a Raycast to Lock on to an enemy but it doesn't seem to be working. Also is it possible to set up an array or list of targets it can lock onto?

function Update(){ var hit : RaycastHit; var fwd = transform.TransformDirection(Vector3.forward);

 if (Physics.Raycast (transform.position, fwd, 100)) {
     if (target.gameObject.tag == "Enemy"){
     transform.LookAt(target);
     }
 }

}

And if possible can someone explain a method of shifting through targets and cancelling the lock on with a button?

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
3
Best Answer

Answer by skovacs1 · Oct 28, 2010 at 02:44 PM

  • You store a hit variable, but you never use it.
  • What is target? When do you set it and what do you set it to?

Whether your raycast hit or not, this code wouldn't do anything.

You probably want something more like:

function Update(){
    var hit : RaycastHit;
    if(Physics.Raycast(transform.position, transform.forward, hit, 100)
       && hit.transform.gameObject.tag == "Enemy")
        transform.LookAt(hit.transform);
}

If you wanted an array of available targets you would create an array of available targets - I think the question is itself the answer. Perhaps you meant to ask something else - maybe about how to set or fill an array of targets or how to use such an array?

var targets : GameObject[]; //You can fill this in the editor too

You could initialize it with something like:

function Start() {
    targets = GameObject.FindGameObjectsWithTag("Enemy");
}

If your array is going to change at run-time, you might in stead consider an Array object and use Array.Add or Array.RemoveAt or Array.Push or Array.Pop or Array.Shift or Array.Unshift to change the content of the array as you need.

var targets : Array; //This will not serialize to the editor

function Start() { targets = new Array(GameObject.FindGameObjectsWithTag("Enemy")); }

To check if your hit object is in the array in either form, you would have to loop through the array.

for(var go : GameObject in targets)
    //do something with go like if(go == hit.transform.gameObject)...
Comment
Add comment · Show 6 · 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 Persona · Oct 28, 2010 at 03:13 PM 0
Share

Well, that was fast and accurate. Thanks.

avatar image Persona · Oct 28, 2010 at 03:26 PM 0
Share

But can you explain how to shift through the targets with a button press?

avatar image skovacs1 · Oct 28, 2010 at 03:42 PM 0
Share

shift through? I assume you mean not to use the raycast method, but to simply lock onto a specific target from the array. That's fairly simple. Store an int variable outside of your functions as your current target and then increment it on button press to change your current target.

avatar image skovacs1 · Oct 28, 2010 at 04:06 PM 0
Share

var targets : Array; private var current : int = 0; private var locked : boolean = false; function Start() {targets = new Array(GameObject.FindGameObjectsWithTag("Enemy"));} function Update() {if(Input.GetButtonDown("Lock")) locked = !locked; if(locked) {if(Input.GetButtonDown("Next Target")) current++; if(Input.GetButtonDown("Previous Target")) current--; current = $$anonymous$$athf.Clamp(current, 0, targets.length-1); if(targets.length > 0) transform.LookAt(targets[current].transform);}}

avatar image skovacs1 · Oct 28, 2010 at 04:14 PM 0
Share

To lock onto targets by proximity, you would have to sort the array somehow or write a function that finds the nth closest object in the array. Sorting might be a bit easier in c# with more complex containers and methods to sort with. Odds are if you go this route, you'd write your own findNthClosestTarget function and call that with current because as you play, the targets may get closer and farther. Also, when using the array method, make sure to remove destroyed enemies and to add new enemies in order to ensure your target array is current.

Show more comments

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make an object to look at Raycast Hit Point? 1 Answer

RAYCAST Forward error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 1 Answer

physics2d.raycastall help please 1 Answer

Scripting to receive a touch over a specific area 0 Answers


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