- Home /
Mathf.SmoothDampAngle Question
I have a question regarding the Mathf.SmoothDampAngle function. I have a plane, which I am trying to tilt randomly in different directions. I want it to be constrained to a maximum x rotation and a maximum z rotation. I do not want it to rotate on the y axis (spin) yet. This is the code I am using, and it does what I want, but it does not do it smooth enough, neither does it seem to use the whole range of the constraints that I put. (Which are 45 degrees in each direction) My code is as follows:
rotX = Random.Range (MIN_ROTATION, MAX_ROTATION);
rotY = Random.Range (MIN_ROTATION, MAX_ROTATION);
rotZ = Random.Range (MIN_ROTATION, MAX_ROTATION);
float xAngle = Mathf.SmoothDampAngle (transform.eulerAngles.x, rotX, ref velocity, smooth, Mathf.Infinity, Time.deltaTime);
float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, rotY, ref velocity, smooth);
float zAngle = Mathf.SmoothDampAngle (transform.eulerAngles.z, rotZ, ref velocity, smooth, Mathf.Infinity, Time.deltaTime);
Vector3 newRot = new Vector3(xAngle, 0.0f, zAngle);
rigidbody.transform.rotation = Quaternion.Euler(newRot);
Does Anyone have any ideas as to what I can change to make the platform tilt back and forth on the x and z axis smoothly with a controllable speed and controllable range?
Answer by Taliesen01 · May 13, 2012 at 01:07 AM
Well noone answered me but I think I might have solved my problem, I will post it in case anyone else might have need of this answer. Basically the problem is that since every update it was generating a random target for the smoothdampangle to tilt to, it never actually reached its goal. That made it look jerky. I solved this rather simply by putting a fixed time step based on delta time which says only recalculate a new target every second.
if(time >= 1){
rotX = Random.Range (MIN_ROTATION, MAX_ROTATION);
rotY = Random.Range (MIN_ROTATION, MAX_ROTATION);
rotZ = Random.Range (MIN_ROTATION, MAX_ROTATION);
time = 0;
}
float xAngle = Mathf.SmoothDampAngle (transform.eulerAngles.x, rotX, ref velocity, smooth, Mathf.Infinity, Time.deltaTime);
float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, rotY, ref velocity, smooth);
float zAngle = Mathf.SmoothDampAngle (transform.eulerAngles.z, rotZ, ref velocity, smooth, Mathf.Infinity, Time.deltaTime);
Vector3 newRot = new Vector3(xAngle, 0.0f, zAngle);
transform.rotation = Quaternion.Euler(newRot);
time += Time.deltaTime;
If any of you can think of a better way of doing this, please post here and tell me.
hello thank you for posting your fix for this i have not found any possible good way to manipulate rotations in unity, i dont know why they make it so hard to do. but nothing really works.
Your answer
Follow this Question
Related Questions
player flicks when mouse is near the player 1 Answer
Clamp Rotation Problem 1 Answer
AngryBots like character rotation system 1 Answer
Joystick Angle Problems 0 Answers