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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
3
Question by SrBilyon · Dec 25, 2010 at 01:22 AM · attacktargetinghominglockon

Need help with a Third-Person Styled Lock-On System

I want to implement a Lock-on/Trigger system that will let the player find the closest enemy, have the player facing the enemy, and having him attack towards the enemy he is targeted to. I also want to display a ring or a targeting icon on the enemy that is targeted.

How can I make it to where I press the targeting button and have it select the closest enemy and make it "the target"?

I had suggestions to where I give the enemies tags called "Enemy" (which i did) and store them into an array, but that wasn't really enough help :P

Also, after defeating the enemy that I manually set as the target in the inspector, I get this error

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

because of the code that allows the player to face the enemy when attacking:

transform.LookAt(Vector3(theTarget.transform.position.x, transform.position.y, theTarget.transform.position.z));

I think 'Transform' is referring to 'theTarget' being destroyed

How do I get the script to check if "theTarget" is null after destroying it or how do I disable the LookAt() line if enemies don't exist on the map?

Comment
Add comment · Show 5
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 _Petroz · Dec 27, 2010 at 06:43 AM 0
Share

I think you should break it down and ask a more specific question rather than saying "I want feature X" here is my code.

avatar image SrBilyon · Dec 27, 2010 at 06:55 AM 1
Share

I did: "a Lock-on/Trigger system that will have the player facing the enemy and having him attack towards the enemy he is targeted to."

Can't really get any more specific than that other than saying: "I want to get the player to look at another object." and "I want the player to dash towards the other object"

A targeting system allows a players actions to be focused toward the object he is targeting at, figured any other details in the question would be pointless.

I provided the code to show what I have completed and an attempt to demonstrate the implementation I was heading towards.

avatar image _Petroz · Dec 27, 2010 at 07:24 AM 1
Share

It's good that you removed the massive block of code from your question. However your questions are still in the context of your code which makes it difficult for readers to understand. If you boil the problem down to a simple specific problem without the context such as: "How do I make an object lookat another object only rotating about Y-axis?" That can be answered easily (and already has been)

avatar image SrBilyon · Dec 27, 2010 at 07:56 AM 0
Share

Okay, I just chopped the question down a bit more. How is it now?

avatar image SrBilyon · Dec 27, 2010 at 08:04 AM 0
Share

BTW, thanks to your last comment, I found I bit of the answer I was looking for: http://forum.unity3d.com/threads/49471-LookAt-to-rotate-only-on-Y-axis

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

Answer by SrBilyon · Dec 27, 2010 at 11:56 AM

Solved everything! (Not by myself entirely, this is a bit embarrassing)

This is the script I wrote thanks to handsomePATT from the forums and an answer skovacs1 gave a while back to another answer (got to give credit where credit is due)

//================================================ //Lock On Script //================================================ private var current : int = 0; private var locked : boolean = false; var playerController : ThirdPersonController ; var enemyLocations : GameObject[]; var closest : GameObject; var activeIcon : Transform; //Current targeted enemy indicator

function Update() {
var playerController : ThirdPersonController = GetComponent(ThirdPersonController);

 if (closest != null && locked)

{ activeIcon.active = true; activeIcon.transform.position.y = (closest.transform.position.y+1); activeIcon.transform.position.x = (closest.transform.position.x); activeIcon.transform.position.z = (closest.transform.position.z); } else {
activeIcon.active = false; }

 if(Input.GetButtonDown("Lock")) 
 {       
 //Looks for the closest enemy
 FindClosestEnemy();
 locked = !locked;
 }

 if(locked) 
 {
     //If there aren't any enemies (or the player killed the last one targeted) make sure that the lock is false
     if (!closest)
     {       
         activeIcon.active = false;
         locked = false;
         closest = null;
     }

     if (playerController.isAttacking)
     transform.LookAt(Vector3(closest.transform.position.x, transform.position.y, closest.transform.position.z));
 }

}

function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); //var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in enemyLocations) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude;

         if (curDistance < distance) 
         { 
             closest = go; 
             distance = curDistance; 
         } 
     } 
 return closest; 

}

Comment
Add comment · 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
0

Answer by Jean-Fabre · Dec 27, 2010 at 07:32 AM

Hi,

Not Actually coming up with a ready answers ( sorry :) ), but just some directions that might be useful.

It looks to me you need to add another hierarchy level between the controller and the player by adding one gameObject inbetween the two, that gameobject will then allow to control the rotation independently from the controller.

This allow you to have the controller moving and looking at its target while you have the inbetween gameObject looking somewhere else.

Anyway, I think for someone to really come up with an answer, you should make a small example, exposing the issue, then I would happily have a look, mess with it to achieve the desired effect. This is the most effective way in my opinion to solve such precise case.

Bye,

Jean

Comment
Add comment · Show 2 · 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 SrBilyon · Dec 27, 2010 at 08:32 AM 0
Share

Alright, I revised my question to reflect on what I actually need.

avatar image Jean-Fabre · Dec 27, 2010 at 10:29 AM 0
Share

isn't part of the answer in the error you get? simply check or build a flag to know whether you should lookat your ennemy in a certain way or not. Or do you actually want to know how to check if a variable is null? Also your revision seems now to boil down to checking where your enemy ares and picking up the closest. am I correct? If that's the case , I can build a quick scene showing how to pick the closest gameobject either from the mousepointer or from an actual position in space. Would that help you progress?

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

LockOn Target System (c#) 1 Answer

lock on enemy and shoot 1 Answer

Homing Missiles that target where you were 0 Answers

Calculate angle between two locations, and then rotate object to face target based on this. 1 Answer

How to make a Homing Attack like in Sonic the HedgeHog? 2 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