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 Xatoku · Mar 01, 2011 at 10:37 PM · cameraenemyfollowlockon

Locking onto Multiple AI's

I'm making a hack n slash. The lock on system I'm currently using will focus the camera on both the character it follows and the enemy click by the mouse. The problem here is that because the camera searches by tag, it will only lock on to the closest enemy. Okay so the way I've fixed this is by making different tags entitled enemy1, enemy2, enemy3, etc. and scripts saying if they've been clicked or not. Is there at all an easier way to do this by just having 1 "enemy" tag? I basically want it so that whichever enemy I click, the camera will follow. The current way Im doing this is very tedious because of the repetition of nearly the exact same scripts.

I won't post the entire codes because they're really long, but I'll give a somewhat basic representation.

3 Enemies in the scene, respectively tagged 1,2, and 3.

On tag Enemy1:

static var HasBeenClicked:boolean = false;

function OnMouseDown() { Enemy1.HasBeenClicked = true; Enemy2.HasBeenClicked = false; Enemy3.HasBeenClicked = false; }

On the Camera to focus on the enemy:

var damp = 20; static var Ready:boolean = true; var rotate;

function Update () { if(Enemy1.HasBeenClicked == true) { rotate = Quaternion.LookRotation(GameObject.FindWithTag("enemy1").transform.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime damp); } if(Enemy2.HasBeenClicked == true) { rotate = Quaternion.LookRotation(GameObject.FindWithTag("enemy2").transform.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime damp); } if(Enemy3.HasBeenClicked == true) { rotate = Quaternion.LookRotation(GameObject.FindWithTag("enemy3").transform.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); } }

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

Answer by by0log1c · Mar 02, 2011 at 12:24 AM

What I'd do is create an array holding reference to all enemy GameObjects. I guess a simple way would be to use:

var allEnemies = FindObjectsOfType(enemyScript).gameObject;
var myTarget:GameObject;

then, in the script catching input, simply set myTarget to the enemy you clicked. Finally,have the camera focus on myTarget.

Plenty of better way, but that's the first I can think of :)

EDIT: Long story short, hold a single target reference rather than have plenty of on/off switch to parse through.

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

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

Camera Focus + Follow 0 Answers

lock ontro enemy 1 Answer

Object lerping in same direction as camera jitters 0 Answers

Want to make a following enemy freeze when visible by main camera 1 Answer

Enemy LockOn and Shoot 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