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 PaxStyle · May 30, 2014 at 01:02 PM · functionmath

Problem with mathf function

Hi to all, In this part of script, if you press D, the object should go up, instead it moves up and down at the same time.. why? Can someone explain me this? Thank you

 void Update () {
 
 if (Input.GetKey (KeyCode.D)) {
      
            arm_rot = (Mathf.Sin (arm_sin) * 30.0f) - 30.0f;
            arm_sin = (arm_sin + Time.deltaTime * 5.0f) % 360.0f;
            arm[0].transform.localEulerAngles = new Vector3 (arm_rot, 0.0f, 0.0f );
      
            }
 }

 
Comment
Add comment · Show 6
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 Nerevar · May 30, 2014 at 01:17 PM 0
Share

That's what the Sin function does, the behaviour is normal.

what movement do you want to achieve?

avatar image Nerevar · May 30, 2014 at 01:25 PM 0
Share

I don't know if that's what you want but try this:

 arm[0].transform.localEulerAngles = new Vector3 (arm_sin, 0.0f, 0.0f );

You need an angle when you assign EulerAngles, you don't need sinus function I guess.

avatar image PaxStyle · May 30, 2014 at 01:43 PM 0
Share

@Nerevar i would like that if i press D the "arm" goes up, and if i press F it goes down.. in this script the movement is right, the only thing that is wrong is when i press D, because the arm should go only UP, ins$$anonymous$$d here it goes up and down at the same time.

avatar image Nerevar · May 30, 2014 at 01:55 PM 0
Share

okay, did you try to just use arm_sin as I posted in previous comment?

Can I see the code for when you press F?

avatar image PaxStyle · May 30, 2014 at 02:08 PM 0
Share

yes i tried to change with arm_sin but the arm goes down at 360°..
here the full script:

 using UnityEngine;
     using System.Collections;
     
     public class Braccio : $$anonymous$$onoBehaviour {
     
         float arm_rot;
         float arm_sin;
     
         public GameObject[] arm;
     
         void Awake()
         {
             arm_sin = 0.0f;
             arm_rot = 0.0f;
     
         }
         
         // Update is called once per frame
         void Update () {
         
     
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Z)) {
     
             arm_rot = ($$anonymous$$athf.Sin (arm_sin) * 30.0f) - 30.0f;
             arm_sin = (arm_sin + Time.deltaTime * 5.0f) % 360.0f;
             arm[0].transform.localEulerAngles = new Vector3 (arm_rot, 0.0f, 0.0f );
     
             }
     
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.X)) {
                 
             arm_rot = ($$anonymous$$athf.Sin (arm_sin) * - 30.0f) - 30.0f;
             arm_sin = (arm_sin + Time.deltaTime * 5.0f) % 360.0f;
             arm[0].transform.localEulerAngles = new Vector3 (arm_rot, 0.0f, 0.0f );
                 
             }
         }
     }
     
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by HarvesteR · May 30, 2014 at 02:56 PM

There are a few things that don't look quite right with your math there... First, you're trying to take a sine from an angle in degrees, while usually trig methods work with Radians.

arm_sin % 360 won't have the expected result here, as it will be treated as a value that is expected to range from 0 to 2PI. 360 in radians is quite a few turns, hehe.

This means you're taking a sine value from a float that, as far as radians go, will just continue to increment. This is commonly used to produce a back-and-forth movement, as sin(t f + p) a is generally known as the sinewave equation, where t is a running time value, f is frequency, p is the phase shift, and a is amplitude.

Your code more closely resembles a sinewave than something meant to increment and decrement an angle. The fact that you're using degrees where radians are expected could also mean you're seeing very fast movement as well, which will look like jitter.

It's hard to be of much more help without knowing exactly what you want to achieve here... Is the sin function being used because you want the arms to gradually ease to a stop at the extremes? Or do you just want linear motion for that X angle rotation? If the latter case is good enough, you could probably just remove the sine function altogether, and just increment/decrement an arm_angle float which is fed straight into the transform euler angles thing (that one uses degrees in fact, not radians).

Cheers

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 Nerevar · May 30, 2014 at 03:28 PM

I got this so far :

 using UnityEngine;
 using System.Collections;
  
 public class Braccio : MonoBehaviour {
  
     public GameObject[] arm;
 
     float speed;
 
     void Awake()
     {
         speed = 10f;
     }
 
     // Update is called once per frame
     void Update () {
     
         // go down
         if (Input.GetKey (KeyCode.Z)) {
         
 
            arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speed);
  
          
         }
        
         // go up
         if (Input.GetKey (KeyCode.X)) {
 
             arm[0].transform.Rotate(-Vector3.right * Time.deltaTime*speed);
  
         }
     
     }
 }

Now you have to set your own limits for the movement (the range angle). Which is another tricky problem by the way :p

Comment
Add comment · 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 PaxStyle · May 30, 2014 at 03:58 PM 0
Share

1)hmm thank you, but in my script the limits there were already.. :o D: why here no?

2) but in your script the var speed is useless? right?

avatar image Nerevar · May 30, 2014 at 04:03 PM 0
Share

Yes sorry, I made an edit about speed.

You can use speed as an extra parameter, if you think it is usefull to you. (for example you could also increase or decrease speed using some input keys)

About the limits: settings limits is complicated with this example. It depends a lot on how your arm axis are oriented and where is 0 on the range angle you use, if it is in the middle it is complicated.

avatar image PaxStyle · May 30, 2014 at 04:40 PM 0
Share

Ok for the limits i will open another question.

how can i do for increase speed when the arm go up or down? Because so is jerky when i press a key.. i would like that the start is gradual for both the movement (up and down)

avatar image PaxStyle · May 30, 2014 at 05:23 PM 0
Share

Online i found this.. is good for me? http://answers.unity3d.com/questions/29751/gradually-moving-an-object-up-to-speed-rather-then.html

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

How to get 0-360 degree from two points 2 Answers

Matrix Multiplication, Standard Deviant and More usefull methods 2 Answers

Function to rotate my Player smoothly taking the y and use it to have angle 1 Answer

How do you make an algebra function for this pattern? 1 Answer

How to do x^-1 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