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 Pascal97 · Jun 07, 2015 at 06:46 PM · c#rotationmovementsincos

Trigonometric functions

I have a cage in my game hanging from the ceiling and I want it to dangle around. Therefor I wrote the following Script:

using UnityEngine; using System.Collections;

public class Cage : MonoBehaviour {

 private float xrot;
 private float zrot;
 private float time;

 void Start()
 {
     xrot = (Random.Range (0, 1000) / 1000.0f) * 10f - 5f;
     zrot = (Random.Range (0, 1000) / 1000.0f) * 10f - 5f;
     time = (Random.Range (0, 1000) / 1000.0f) * Mathf.PI;
 }
 
 void Update () {
     if(Time.time - time >= 0){
         transform.RotateAround (new Vector3 (transform.position.x, 12, transform.position.z), new Vector3 (1, 0, 0), xrot * Time.deltaTime * Mathf.Cos (Time.time - time));
         transform.RotateAround (new Vector3 (transform.position.x, 12, transform.position.z), new Vector3 (0, 0, 1), zrot * Time.deltaTime * Mathf.Cos (Time.time - time));
     }
 }

}

The Code choses a random x and z rotation speed (positive or negative) and uses a cosine to rotate the cage. When the cage is instantiated it has a rotation of 270 and hangs straight down from the ceiling which is the reason why is has to have full speed at the beginning since it starts a the lowest and fastest part of the swing motion. The problem was all cages started swinging at the same time so I used "time" to start them at a different velocities. Now I do not know how to correct the start rotation of the cage. Having a random starting velocity demands the corresponding start rotation since those values are connected. I solved the poblem not very nice by letting them start to swing after the random delay but if you now look at the cage at the very beginning of the game they first do not move and then start abruptly which I do not want :D So is there any way how I can calculate the starting rotation for my cages with the value of "time"?

Thanks in advance! :D

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Jun 07, 2015 at 07:55 PM

If all else fails, I think you could run a random amount of Updates for each cage when they are created to "fast forward" their position to a random spot.

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 Dave-Carlile · Jun 07, 2015 at 08:04 PM

How about using a Quaternion.Slerp? It takes from rotation, to rotation parameters, as well as t which you change from 0 to 1 over time. When t is 0 it returns from rotation, and when t is 1 it returns to rotation, and when t is in between it returns the appropriate value between from and to.

Your script would need to change t over time, and when it reaches 1 you would reverse the direction and move it back toward 0, and then reverse and back toward 1, and so on.

Once you have that set up, all you need to do for a random start position is to start t at a random value between 0 and 1. It would also be simple to have different swing speeds by varying how fast you modify t.

Edit: You could also use a tweening library from the asset store for changing the value of t, or mechanim, or it's fairly simple to handle yourself in Update or a Coroutine or something.

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

21 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotating Character 1 Answer

Orbit Camera Around Player And Add Force 3 Answers

Rotate towards movement direction 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