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 ElectricToothbrush · Jun 22, 2010 at 06:58 AM · gameobjectrotatedirectionnearest

Locking onto enemies with the third person controller

Hi there. I've got a simple scene setup with a player using the third person controller from the 3D Platformer tutorial and an enemy. I've got the code found at the bottom here in a function that a I call in LateUpdate with GetButton so that while I hold the button down, the player "locks on" and continuously faces the enemy while moving around him not unlike various 3D action games like Phantasy Star Online, Devil May Cry, etc.

function Update () { var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoint"); var closest: GameObject; var closestDist = Mathf.Infinity;

 for (waypoint in waypoints) { 
     var dist = (transform.position - waypoint.transform.position).sqrMagnitude; 

     if (dist < closestDist) { 
         closestDist = dist; 
         closest = waypoint; 
         } 
 } 
 transform.LookAt(closest.transform); 

}

This works almost perfectly. The problem is, when you let go of the button, the player's facing immediately snaps back to the direction you were moving in. So, for example, if you were locked on and moved away from the enemy then let go of the button, the player's facing immediately snaps away from the enemy. I'm assuming this is because LookAt isn't affecting what the Third Person Controller's using to rotate the character's facing in the direction its moving (moveDirection?), however, I'm unsure as to how to use the enemy's position with the movement direction so that when you let go of the button, the player still faces the enemy at that moment until you move him in a different direction. I'd appreciate any help I could get!

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

2 Replies

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

Answer by AnaRhisT · Jun 22, 2010 at 09:02 AM

while(Vector3.Distance(enemy.position, transform.position) > 0.1){
   var direction : Vector3 = transform.position - enemy.position;
    transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), 0.2* Time.deltaTime);
    transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
    yield;
    }

You can change the while loop to something like while(Enemy is visible on the camera) or whatever. The code above will give u a basis to what u need.

Comment
Add comment · Show 3 · 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 ElectricToothbrush · Jun 22, 2010 at 01:08 PM 0
Share

Hi, thanks for responding. I put the code inside the function I call in LateUpdate and the problem still persists. The player immediately faces the direction you were last moving in.

avatar image ElectricToothbrush · Jun 23, 2010 at 03:22 AM 0
Share

Just wanted to update that this was indeed the basis for what I needed. I put this in my function and then used GetButtonUp to add a tiny invisible amount of movement in the direction of the enemy in Update before UpdateSmoothed$$anonymous$$ovementDirection is called. This keeps the player facing in the direction of the enemy after you stop locking onto them. There's a some amount of unwanted rotation on the x and z axis that happens as you lock on and off that goes away immediately while moving and does cause a bit of strange behavior, but I'll just have to iron that out. Thanks a ton!

avatar image AnaRhisT · Jun 23, 2010 at 03:44 AM 0
Share

sure... you're welcome :)

avatar image
0

Answer by taib · Jul 18, 2020 at 05:00 PM

after 10 years i reply, just use bool like.. if (input.getbuttondown ("fire1") { isLocked = true; } dont know if this question still available or not.. sory for my bad English ahaha

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

1 Person is following this question.

avatar image

Related Questions

How can I make my gameObject find the nearest object with a specific tag and rotate to look at it? 6 Answers

Problem with the direction of the raycast when rotating 1 Answer

rotate to swip direction 0 Answers

Rotation is jumpy 2 Answers

C# Rotate GameObject at Other Point other than Center 3 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