- Home /
Arm Rotation.
Hi guys, i did this script to make a right rotation (if press A, increasing right speed) and left rotation (if press D increasing left speed), but the script works bad. The rotation is jerky.. If i remove the keys input, the script works well, so the problem is there.. hope yu can help me, any help is good. Thank you.
using UnityEngine;
using System.Collections;
public class Gondola : MonoBehaviour {
public GameObject gondelarm;
float gondelarm_sin;
void Awake()
{
gondelarm_sin = 0.0f;
}
void Start()
{
}
void Update()
{
//-- right arm rotation
if(Input.GetKeyDown(KeyCode.A)){
float arm_rot = Mathf.Sin(gondelarm_sin) * 80.0f;
gondelarm_sin += (gondelarm_sin + Time.deltaTime * 1.0f) % 360.0f;
gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
}
//-- left arm rotation
if(Input.GetKeyDown(KeyCode.D)){
float arm_rot = Mathf.Sin(gondelarm_sin) * 80.0f;
gondelarm_sin -= (gondelarm_sin + Time.deltaTime * 1.0f) % 360.0f;
gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
}
//-- brake arm rotation
if(Input.GetKeyDown(KeyCode.S)){
float arm_rot = Mathf.Sin(gondelarm_sin) * 80.0f;
gondelarm_sin += (gondelarm_sin + Time.deltaTime * 0.9f) % 360.0f;
gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
}
}
}
A couple of things jump out a me. You are mixing degrees and radians. $$anonymous$$athf.Sin() returns radians. Second, can't you just use:
transform.Rotate(0f, 0f, speed * Time.deltaTime)
...and have your A and D keys increment/decrement 'speed'?
Your original question only mentions rotation, but with the use of $$anonymous$$athf.Sin() what you are really seem to be after is something that swings back and forth. Correct?
Okay, so the object is swinging. What is this left vs right speed thing? You want it to swing faster in one direction than the other? And what is this 'brake arm rotation'? Does that slow both sides down?
So.. i try to explain, If i press A, add force to right, If I press D, add force to left. If I press S, decrease the current speed, until stop it.
So, this script works well, but missing the input, and the increasing of the speed. (That i tried to add in the code that i've posted before), but the movement that i would like are so.
using UnityEngine;
using System.Collections;
public class Gondola : $$anonymous$$onoBehaviour {
public GameObject gondelarm;
float gondelarm_sin;
void Awake()
{
gondelarm_sin = 0.0f;
}
void Start()
{
}
void Update()
{
//-- arm rotation
float arm_rot = $$anonymous$$athf.Sin(gondelarm_sin) * 80.0f;
gondelarm_sin = (gondelarm_sin + Time.deltaTime * 1.0f) % 360.0f;
gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
}
}
Answer by robertbu · Apr 03, 2014 at 07:13 PM
In Unity, typically a problem like this one would be handled by the physics engine. Simulating physics like this without some substantial research will be hit and miss. Here is a bit of code in the direction of what I think you are trying to do. It's not perfect, and you may have to do substantial additional work to get what you want, but it should provide you a starting point.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public GameObject gondelarm;
private float gondelarm_sin = 0.0f;
public float gondelSpeed = 1.0f;
public float increaseFactor = 0.5f;
public float maxAngle = 80f;
public float breakingFactor = 0.9f;
private float prevRot = 0.0f;
private bool right = false;
void Update()
{
//-- right arm rotation
if(Input.GetKeyDown(KeyCode.A))
{
if (right)
gondelSpeed -= increaseFactor;
else
gondelSpeed += increaseFactor;
}
//-- left arm rotation
if(Input.GetKeyDown(KeyCode.D))
{
if (right)
gondelSpeed += increaseFactor;
else
gondelSpeed -= increaseFactor;
}
//-- brake arm rotation
if(Input.GetKeyDown(KeyCode.S)){
gondelSpeed *= breakingFactor;
}
gondelarm_sin += gondelSpeed * Time.deltaTime;
float arm_rot = Mathf.Sin(gondelarm_sin) * maxAngle;
right = prevRot < arm_rot;
prevRot = arm_rot;
gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613141801im_/https://answers.unity.com/themes/thub/images/avi.jpg)