Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by M4koren · Mar 21, 2017 at 12:40 PM · rotationtransformvector3quaternionslerp

How to use Quaternion.Slerp with transform.LookAt?

I'm creating a game where you need to "lock on" to your target in order to properly aim your attacks. I've already made working code where you can move your player around, make him jump, and make him lock on to the enemy.

The problem is, whenever I lock onto the target, the player object rotates immediately. It finishes the rotation in a single frame. In a slightly more realistic setting, you would look at the target by turning around over the span of half a second or so. This is what I want to achieve with the player object.

From what I understand, Quaternion.Slerp is a good tool for this kind of thing. I already use it in my player movement:

 float moveHorizontal = Input.GetAxisRaw ("Horizontal");
         float moveVertical = Input.GetAxisRaw ("Vertical");
 
         if (moveHorizontal > 0 || moveHorizontal < 0 || moveVertical > 0 || moveVertical < 0)
         {
             Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
 
             transform.Translate (movement * moveSpeed * Time.deltaTime, Space.World);
         }

But I can't seem to make it work with my lock on code.

Here's my current lock on code, working version without all the Slerp experiments:

 if (lockOnToggle == "yes")
         {
             Vector3 lockOnLook = new Vector3 (enemy.transform.position.x, transform.position.y, enemy.transform.position.z);
             transform.LookAt(lockOnLook);
         }

Both of these blocks of code are in void Update().

I've tried several methods by googling, such as converting Vector3 to Quaternion and other crazy code stuff, but all the results that came up were confusing and I couldn't get them to work.

How would I go about making this work?

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

3 Replies

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

Answer by doVa09 · Mar 21, 2017 at 04:01 PM

Try:

   if (lockOnToggle == "yes")
   {
 Quaternion lookOnLook =
 Quaternion.LookRotation(enemy.transform.position - transform.position);
 
        transform.rotation =
        Quaternion.Slerp(transform.rotation, lookOnLook, Time.deltaTime);
    }
 

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 M4koren · Mar 21, 2017 at 09:35 PM 0
Share

Perfect, thank you.

avatar image Br00dl0rd · Mar 31 at 07:12 PM 0
Share

Awesome, that worked for me, thanks!

avatar image
0

Answer by mfatihbarut · Feb 14, 2021 at 07:09 PM

thanks charmingly working

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 BostonGamesLLC · Jul 12, 2021 at 03:13 AM

This worked for me as well, thank you!

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

125 People are following this question.

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

Related Questions

Can't really grasp how to keep my player level on X and Z while using LookRotation 0 Answers

Why isn't my steering wheel working? 0 Answers

unwanted rotation in y axis 0 Answers

how to convert a Vector3 to a float value? 1 Answer

Quaternion rotation issue please help :'( 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