• 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
Question by jjplay175 · Sep 16, 2014 at 10:36 AM · rotationquaternionslerp

Quaternion Slerp help

I've been looking around and even if I use the exact code people are using I still can't get this to work for the life of me so I've been just working on my own code to no avail

 public float speed;
 private Transform CurrentPos;

 void Start() {
     CurrentPos = transform.rotation;
 }

 void OnColisionEnter () 
 {
     CurrentPos = Quaternion.Slerp(CurrentPos, Quaternion.Euler(0,90,0), Time.deltaTime * speed);
 }

I get an error after start on line 5

 CurrentPos = transform.rotation;

error CS0029: Cannot implicitly convert type UnityEngine.Quaternion' > to UnityEngine.Transform'

I then get 2 errors on line 10

 CurrentPos = Quaternion.Slerp(CurrentPos, Quaternion.Euler(0,90,0), Time.deltaTime * speed);

The best overloaded method match for UnityEngine.Quaternion.Slerp(UnityEngine.Quaternion, > UnityEngine.Quaternion, float)' has > some invalid arguments > error CS1503: Argument #1' cannot convert UnityEngine.Transform' > expression to type > UnityEngine.Quaternion'

No matter how I try to approach it I get errors, I can't seem to grasp Quaternion by the looks of it, I have tried setting it to Transform.Rotation,Y = 90 as the target position but I still get errors

I understand that Quaternion uses X,Y,Z,W but it even says on the Scripting API that you can use Quaternion.Euler which gives a Vector3 and you still get an error

I feel unity API could explain these a lot better, from what I'm reading none of the methods seem to work

Comment

People who like this

0 Show 2
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 Dhiru · Sep 16, 2014 at 11:11 AM 0
Share

Try This:

 private Vector3 currentAngle;
 
 void Start() {
     currentAngle = child.transform.localEulerAngles;
 }
 
 void OnColisionEnter(){
     transform.localEulerAngles = Vector3.Slerp(currentAngle, new Vector3( 0, 90, 0), Time.deltaTime * speed);
 }


You should use OnCollisionStay, with Slerp(), instead of OnCollisionEnter/Exit.

avatar image jjplay175 · Sep 16, 2014 at 04:09 PM 0
Share

I changed it to void OnTriggerStay() and it works and I get the message that the collision happened but it only happens once, it doesn't update it to 90 it just does 1 frame then stops

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bluntweapon · Sep 16, 2014 at 05:00 PM

The following code assumes you've already changed to OnCollisionStay, and that this script is attached to whichever object you want to rotate on contact.

 public float speed;
 
 private Quaternion targetRotation;
 
 void OnCollisionEnter(){
     //rotation * euler(0,90,0) should give you the current rotation rotated 90 degrees
     targetRotation = transform.rotation * Quaternion.euler( 0, 90, 0 );
 }
 
 void OnCollisionStay () 
 {
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
 }

You'll notice you don't really need whatever was in Start(), since calling transform.rotation is basically the same.

EDIT: I just realized something. OnCollisionStay is called every frame something stays in the collider. If you just destroy the colliding object then it makes sense that OnCollisionStay only fires off once and you only rotate for 1 frame.

EDIT 2: Man I don't know how I totally confused rotations for positions...

Comment
jjplay175

People who like this

1 Show 4 · 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 jjplay175 · Sep 16, 2014 at 05:17 PM 0
Share

When I have it onTriggerStay the guys goes through the box which is what I want but after that it just stays

http://puu.sh/bBxeu/4f3fa47eb3.png

As you can see the box is still there but it's not moving the door more than the 3 it already did

avatar image Bluntweapon · Sep 16, 2014 at 06:21 PM 0
Share

Ignore what I said. I apparently confused rotations for positions.

Edited above answer.

avatar image jjplay175 · Sep 16, 2014 at 07:40 PM 0
Share

I changed it to update and I noticed that it didn't move, it just stuck depending on what the speed was

I managed to get it working in a way that is better for the purpose of opening a door by using

 void OnTriggerEnter()
 {
     Debug.Log ("Checking If Req Met");
     Debug.Log (frontKey);
     if(frontKey == 1)
     {
         doorOpen.enabled = true;
     }
 }

And my doorOpen is

 void Start() {
     currentAngle = rightDoor.localEulerAngles;
     currentAngle = leftDoor.localEulerAngles;
 }
 
 void Update()
 {
     timeTime = timeTime + Time.deltaTime;
     rightDoor.localEulerAngles = Vector3.Slerp(currentAngle, new Vector3( 0, 90, 0), timeTime * speed);
     leftDoor.localEulerAngles = Vector3.Slerp(currentAngle, new Vector3( 0, -90, 0), timeTime * speed);

     if (rightDoor.eulerAngles.y >= 90)
     {
         this.enabled = false;
     }
 }

This seems to work quite well, after the door is open it disables itself to stop Update draining resources

I will also try your code tomorrow to see if that works, might help with future scripts

avatar image Bluntweapon · Sep 16, 2014 at 08:13 PM 0
Share

This way is probably better, as it doesn't depend on colliders being in other colliders.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

Quaternion.Slerp and Quaternion.Euler problem 1 Answer

Rotation Slerp Never ending... 2 Answers

Quaternion.Slerp problem... 1 Answer

Choppy rotation of character around y-axis 1 Answer

Rotate along single access toward object 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