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 DeadKenny · Aug 22, 2013 at 11:46 PM · c#collisionvector3quaterniondirection

Slerp look at direction not working...

Yo this is a very simple problem, hopefully.

I can't get this AI of mine to look at the direction of a collision smoothly. He snaps at it.

My usual code that I have used for the AI following does not work. It gives no errors but the AI does not detect the bullet collision or simply can't turn.

Code:

 // This is the code that works but I cannot get to slerp or be smooth.
 
 void Searching(){
 
 transform.rotation = Quaternion.LookRotation(targetDirection);
 
 
 }
 
 
 // This is where the targetDirection comes from.
 
 void OnTriggerEnter(Collider colp){
 
    targetDirection = colp.transform.postion-transform.postion;
 
 
 
 } }


Can't get the dam thing to slerp.

This does not work:

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.deltaTime);
Comment
Add comment · Show 8
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 DeadKenny · Aug 23, 2013 at 12:01 AM 0
Share

err...

Searching(); is called in Update. Also like Following();

Following works fine as following.

It seems like the Vector3 of targetDirection maybe?

This is a late reply... Busy reading the answers.

avatar image meat5000 ♦ · Aug 23, 2013 at 12:03 AM 0
Share
 transform.rotation = Quaternion.LookRotation(targetDirection);

so

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.deltaTime);

=

 transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation, navSpeed * Time.deltaTime);

avatar image DeadKenny · Aug 23, 2013 at 12:16 AM 0
Share

wait wait where does the targetDirection now?

avatar image meat5000 ♦ · Aug 23, 2013 at 12:29 AM 0
Share

All Lerps and Slerps go

(From, To, t)

Quaternion.LookRotation(targetDirection); should be fine as 'To' but you are already making transform.rotation equal to it in one step before you do the Slerp.

avatar image DeadKenny · Aug 23, 2013 at 12:33 AM 0
Share

Ooooooooooooh...

Ok well my brain is dead for today.

Thanks for help so far I will test tomorrow.

Show more comments

3 Replies

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

Answer by sdgd · Aug 23, 2013 at 12:32 PM

 public GameObject Player; // this is the object you will be rotating
 public GameObject Target; // this is the object you will look at
 
 void Update(){
      Player.transform.rotation = Quaternion.Slerp(Player.transform.rotation, (Quaternion.LookRotation(Target.transform.position - Player.transform.position)), Time.deltaTime * 2);
 }

SLerp Manual

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 DeadKenny · Aug 23, 2013 at 01:50 PM 0
Share

Solved!!!!! Wooohoooo.

avatar image DeadKenny · Aug 23, 2013 at 01:52 PM 0
Share

Thanks, though I didn't do it exactly as that, my target was a vector and transform already.

Anyway it worked.

avatar image sdgd · Aug 23, 2013 at 02:01 PM 0
Share

am thanks, ... could you accept the previous answer please, ...

you closed the Q and said you accepted it but you didn't, ...

I'll keep an eye on you few more times so I can help you more :)

and gain more karma with being specific and accurate with answer.

avatar image
0

Answer by meat5000 · Aug 22, 2013 at 11:51 PM

A Slerp, like a Lerp is not performed in one function. You must attend to the function frame after frame gradually changing the value of t. Note that t ONLY works between 0 and 1. So when navSpeed * Time.deltaTime is Greater than 1 the Slerp will be at its destination. What is the value of navSpeed?

Also your

transform.rotation = Quaternion.LookRotation(targetDirection);

so if you call Searching() before your Slerp is performed then in your Slerp your From and To fields are already equal.

On top of that, I have a feeling that if it did Slerp you wouldnt reach your destination:

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.deltaTime);

Each frame your transform.rotation is updated to be the Current Position of the result of the Slerp. This late at night Im not sure but my pulverised brain is telling me you'll slow down as you get closer? I could be wrong :/

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 sparkzbarca · Aug 22, 2013 at 11:57 PM

as noted for slerp to work it needs to be in update or fixedupdate heres your correct code. Have a nice day!

 bool Search;
 
 void Start()
 {
 Search = false;
 }
 
 void Searching()
 {
 Search = true;
 }
 
 Void FixedUpdate()
 {
 if(Search)
 {
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.fixedDeltaTime);
 }
 
 
 if (transform.forward == targetDirection)
 search = false;
 }
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

18 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

Related Questions

Checking vector3 gameObject is about to move to (pathfinding collision detection) 1 Answer

Quaternion amount/value of rotation? 2 Answers

Setting up point sets for a procedural tree 1 Answer

Snap a direction vector 1 Answer

Object snapping to floor and other objects in VR 1 Answer


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