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 Yonlop · Apr 13, 2012 at 10:05 AM · quaternionlookatnpcsmoothing

NPC LookAt smoothing

Hello there. I've been trying to make a script that allows NPC to look at the character when they are in the trigger zone. So far I have been able to make them do so, but whenever the character goes out of the zone, the NPC snaps back to the initial position immediately. How do I smooth out the movement?

This is the script I'm using.

 var targetObj: Transform;
 var head: Transform;
 var point: Vector3;
 var initialhead: Quaternion = Quaternion.identity;
 var speed = 0.1;
 var resethead: boolean;
 var last: Quaternion;
 
 function OnTriggerStay() {
     point = targetObj.position;
     head.transform.LookAt(point);
     
 }
 
 function Update() {
     if (resethead == true);
     head.transform.rotation =
     Quaternion.Lerp(last.rotation, initialhead.rotation, Time.deltaTime*speed);
 }
 
 function OnTriggerExit() {
     head.transform.rotation = last;
     resethead = true;
     
 
 }

  

Thanks in advance.

Comment
Add comment · Show 1
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 Adamcbrz · Apr 13, 2012 at 12:52 PM 0
Share

there are several things wrong with this script. First thing Quaternion does not have variable for rotation. So the values in your Lerp won't work. Next you are never setting "last" so your lerp does nothing except set the rotation back to Quaternion.identity. Also in OnTriggerExit you are setting your "head.transform.rotation = last" which is setting it to Quaternion.identity. Adjustments to these issues.

 function Update() {
     if(resethead) //since resethead is a boolean you don't have to compare it to true if it is true it will continue (not wrong just preference)
     {
         head.transform.rotation = Quaternion.Lerp(last, initialhead, Time.deltaTime * speed);
     }
 }
 
 function OnTriggerExit()
 {
     last = head.transform.rotation;
     resethead = true;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by crocodile5 · Apr 13, 2012 at 01:17 PM

I use iTween for basic look rotations and turning. Very easy to use to make simple damped turning of objects and some other stuff. Hope it helps you out.

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 Yonlop · Apr 13, 2012 at 03:56 PM

Thanks for the answers. I actually scrapped most of the previous script and figured out this script which works. And now I just need to make the LookAt smooth instead.

 var targetObj: Transform;
 var head: Transform;
 var point: Vector3;
 var speed = 10;
 var target = Quaternion.Euler(0,0,0);
 
 function Update() {
 //this part is to detect player in front of object
     if (targetObj) {
         var forward = transform.TransformDirection(Vector3.forward);
         var toOther = targetObj.position - transform.position;
         print (Vector3.Dot(forward,toOther) );        
         if (Vector3.Dot(forward,toOther) > 0.8 && Vector3.Distance(targetObj.position, transform.position) < 2)
         head.transform.LookAt(targetObj);

//returns the object to initial rotation else head.transform.rotation = Quaternion.Slerp (head.transform.rotation, target, Time.deltaTime * speed); }

I just started scripting today and I am really trying to figure out most stuff.. and my head is quite overloaded now.

Thanks again for the quick answers!,Thanks for the answers. I actually scrapped most of the previous script and figured out this script which works. And now I just need to make the LookAt smooth instead.

 var targetObj: Transform;
 var head: Transform;
 var point: Vector3;
 var speed = 10;
 var target = Quaternion.Euler(0,0,0);
 
 function Update() {
     if (targetObj) {
         var forward = transform.TransformDirection(Vector3.forward);
         var toOther = targetObj.position - transform.position;
         print (Vector3.Dot(forward,toOther) );        
         if (Vector3.Dot(forward,toOther) > 0.8 && Vector3.Distance(targetObj.position, transform.position) < 2)
         head.transform.LookAt(targetObj);
         
         else head.transform.rotation = Quaternion.Slerp (head.transform.rotation, target, Time.deltaTime * speed);
         }

I just started scripting today and I am really trying to figure out most stuff.. and my head is quite overloaded now.

Thanks again for the quick answers!

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

6 People are following this question.

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

Related Questions

Lookat offset angle combined with RotateAround. Basically orbit + rot angle 3 Answers

rotate camera to face a specific object? 1 Answer

Problem with Quaternion.LookRotation() 1 Answer

2d game - looking toward velocity for rigidbody player 1 Answer

Merge a rotation(vector3) to a lookAt(quaternion) ? 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