Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by eitanwass · Aug 25, 2016 at 09:35 AM · rotationarrayrotatetimerspeed

Rotate from current rotation to another over time problem. please help.

I want to make an object rotate to the saved direction of another object and it doesn't work as expected.

CODE:

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 
 public class TimeReflectionRotations : MonoBehaviour {
 
     public Quaternion[] playerRotations;
     public float[] playerRotationsChangeTime;
     public float speed;
     public float time;
     public Vector3 pointToGo;
     public float positionTimeNumber;
 
     [SerializeField]private bool rotating;
     private float degPerSecY;
 
     void Start () {
         
     }
 
     void Update () {
         if (this.transform.position != this.GetComponent<TimeReflectionPositions> ().pointToGo) {
             if (time <= playerRotationsChangeTime.Last ()) {
                 if (playerRotationsChangeTime.Contains (time)) {
                     float timeToCompleteRotation = Vector3.Distance (this.transform.position, this.GetComponent<TimeReflectionPositions> ().pointToGo) / speed;
                     print (timeToCompleteRotation);
                     degPerSecY = (this.transform.rotation.y - playerRotations [System.Array.IndexOf (playerRotationsChangeTime, positionTimeNumber)].y) / timeToCompleteRotation;
                     print (degPerSecY);
                     rotating = true;
                 }
             }
         } else {
             rotating = false;
         }
         if (rotating) {
             this.transform.Rotate (0, degPerSecY * Time.deltaTime, 0);
             print ("Rotate to: (0, " + degPerSecY * Time.deltaTime + ", 0)");
         }
     }
 }
 

Please help me. If you have any questions you can ask below and i'll answer as fast as i can.

Ethan

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

1 Reply

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

Answer by dhore · Aug 26, 2016 at 05:56 AM

Your code seems very overly complicated...

You should be using something like Quaternion.Slerp.

This example will rotate 90 degrees to the right:

 public float rotSpeed;
 Quaternion startRot, endRot;
 
 void Start()
 {
     startRot = Quaternion.LookRotation(transform.forward);
     endRot = Quaternion.LookRotation(transform.right);
 }
 
 void Update()
 {
     if (transform.rotation != endRot)
         transform.rotation = Quaternion.Slerp(startRot, endRot, Time.time * rotSpeed);
 }

You can also use Vector3.Lerp for positions (I noticed a reference to a position script).

Another option (which would give you better results too) would be to use the iTween Plugin, which is free on the Asset Store [link here] and has some decent documentation as well [docs here].

Comment
Add comment · Show 7 · 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 eitanwass · Aug 26, 2016 at 07:11 AM 0
Share

I looked at it and i think it will REALLY help me with my project but i have one more question: Is it possible to add nodes to a path i create through a script? if so, how?

Tx for the quick respond. Ethan

avatar image dhore eitanwass · Aug 26, 2016 at 07:49 AM 0
Share

You mean something like this?

 int goingToPosIndex = 0;
 public Vector3[] path;
 Vector3 myStartPos;
 public float speed;
 
 void Start()
 {
     myStartPos = transform.position;
 }
 
 void Update()
 {
     if (goingToPosIndex < path.length)
     {
         if (transform.position != path[goingToPosIndex])
         {
             Vector3 startPos = Vector3.zero;
             if (goingToPosIndex == 0)
                 startPos = myStartPos;
             else
                 startPos = path[goingToPosIndex - 1];
             
             transform.position = Vector3.Lerp(startPos, path[goingToPosIndex], Time.time * speed);
         }
         else
             goingToPosIndex++;
     }
 }
avatar image eitanwass dhore · Aug 26, 2016 at 08:04 AM 0
Share

@dhore I think i didn't give you all the information, my bad. I have a game object that records every period of time the location of the player and after the player press a button there is a cube that goes at the recorded "trail" of points. I was also looking at iTweens visual path and i think that it would be very helpful to be able to visualize the path the cube will go as a path.

So what i'm asking is, is it possible to "record" the location of the player as points in a path?

Show more comments

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

82 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

Related Questions

How can I set my object rotate speed with this code? 0 Answers

How to change the speed of an audio without affecting its pitch and vice a versa at runtime? 2 Answers

Check rotation of an obstacle 0 Answers

Rotate Player w/Touch Input Controls 0 Answers

rotate in the direction of the child 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