- Home /
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 );
}
}
That's what the Sin function does, the behaviour is normal.
what movement do you want to achieve?
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.
@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.
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?
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 );
}
}
}
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
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
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?
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.
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)
Online i found this.. is good for me? http://answers.unity3d.com/questions/29751/gradually-moving-an-object-up-to-speed-rather-then.html