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 JokerMartini · Jul 20, 2012 at 12:57 AM · positionobjectmatharcsin

arc object's position using sin

How would I convert this into Javascript. Right now this work inside of 3ds Max as is.

the number 12 references to 12 frames a second. So the for loop goes frame by frame. the number 20 refers to how high on the y position the object will go at it's peak

for i = 0 to 12 do ( y = abs(sin(180.0*i/12))*20.0 )

In unity I'm not sure how to right it out since there is the time.deltaTime thing. For my case I want the arc to take course of the life of 2 seconds.

My attempt in javascript


 private var counter = 0.0;
 
 function Start () { 
  InvokeRepeating("IncreaseCounter", 1.0, 1.0); //adds X to 'counter' each second
 }
 
 function IncreaseCounter () {
     counter += 1.0;
 }
 
 function Update () {
     stepY = ( Mathf.Abs( Mathf.Sin(180.0*counter / Time.deltaTime)) * 5.0);
     transform.position.y = stepY;
 }
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
0
Best Answer

Answer by Owen-Reynolds · Jul 20, 2012 at 03:36 AM

o The math keys of off the angle going from 0 to 180. To get it to take 2 seconds, set the angle to: 180*timePassed/2. Check: when timePassed is 2, 180*2/2 = 180.

o Real trig uses radians, not degrees, so before you use sin you have to convert by multiplying the angle by Math.DegToRad (which is really just 2PI/360.)

o To get the time that passed, grab the current time, and count from there:

 var startTime : float;

 // In Start:
 startTime = Time.time; //  Grab the time the shot started

 // In Update:
 var timePassed : float = Time.time - startTime;

The you can use timePassed and the 2-second angle in radians in your sin function. Of course, lots of other ways to write the same thing.

Comment
Add comment · Show 6 · 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 JokerMartini · Jul 20, 2012 at 05:33 PM 0
Share

$$anonymous$$akes complete sense I've got what you've mentioned mostly implemented. I was hoping you could look over it and help me out. I just don't quite get the arc to happen over the course of 2.0 seconds just right. I got rather lost when you talked about implementing the radians ins$$anonymous$$d of degrees. I was hoping you could take a look at what I have so far and help me figure out what I'm missing. Just copy and paste this snippet below and place it on a sphere or object. Thanks

pragma strict

private var stepY = 0.0;

var startTime : float;

var spawnTrgts : GameObject[];

function Start () {

Destroy(gameObject, 2.0);

startTime = Time.time; // Grab the time the shot started

}

function Update () {

var timePassed : float = Time.time - startTime;

stepY = ( $$anonymous$$athf.Abs( $$anonymous$$athf.Sin(180.0*timePassed / 2.0)) * 5.0);

transform.position.y = stepY ;//(2 * Time.deltaTime); }

avatar image Owen-Reynolds · Jul 21, 2012 at 01:38 AM 0
Share

Look up Radians (anywhere, Wikipedia) for some background, then maybe Unity $$anonymous$$athf.

The input to $$anonymous$$athf.sin is NOT the angle in degrees. It's the angle in Radians. You have to multiply the angle in degrees (`180*time/2`) by $$anonymous$$athf.Deg2Rad to convert.

Since a circle has 6.28 radians, using degrees by mistake makes it spin or vibrate about 60 times too fast.

avatar image JokerMartini · Jul 21, 2012 at 01:46 AM 0
Share

So taking the above function where do I add in, and what do I add in, to make it properly work. I've got everything you said in there except for the radians part. I was unclear how to get that integrated into the function.

Thanks for all your help Owen.

avatar image Owen-Reynolds · Jul 21, 2012 at 03:11 PM 0
Share

$$anonymous$$athf.Deg2Rad is just a single, unchanging number. You don't give it any inputs or anything. You just multiply degrees by it. Look it up in the Unity manual for an example.

Not trying to be obtuse, but I assume you're going to want to fiddle with this more, so need to have done some assembly yourself.

avatar image JokerMartini · Jul 23, 2012 at 11:03 PM 0
Share

Alright, I'll check out and see what I can get. I've got the x and z positions working properly. The last part is figuring out the y "arching" position using sin. I'll be looking into the deg2Rad things over the next day or so and I'll let you know how it goes. So far the script does exactly what I want except for the arch. But like you mentioned it currently vibrates about 100 times to fast. I'll be sure to post here the final results once I get it working.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to segment a number line of game objects between two points dynamically? 1 Answer

Save object position (random) 1 Answer

How to make a circle with three known points 4 Answers

Make a navmesh object start moving in a circle around an object 0 Answers

Swapping object position? 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