- Home /
How to rotate your device and counter rotate camera
I'm looking to use accelerometer input to influence game objects, but counter rotate the camera so the scene always stays flat on the horizon. Sort of like asphalt7 and real racing. I have tried a few things but they are all so jittery that it seems like I have frame rate issues. I found this out on the forums somewhere and it gets pretty close but is still not smooth enough.
function Update () {
var finalAngle = Input.acceleration.y*90;
transform.localEulerAngles.z = finalAngle;
}
Is there a better way to go about this? Or how would one go about smoothing the above enough to make things look good?
did you try use smooth damp ?
fuction Update(){
var finalAngle = Input.acceleration.y * 90;
var smoothTime : float = 0.3F;
private var myVelocity : float = 0.0F;
var newPosition : float = $$anonymous$$athf.SmoothDamp(transform.localEulerAngles.z, finalAngle, myVelocity, smoothTime);
transform.localEulerAngles = Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, newPosition);
}
Thank you for the reply. The issue that I am running into with that is the camera is rolling completely over. Like a barrel roll. Im trying to limit it. It is strange because it only does that when I tilt to the left.. the right is fine. The smooth damp does help it out as far as animation goes. It looks really nice in motion. It seems like when the z rotation hits 0 it rolls completely over. Hmm.
Okay, I have a pretty good solution for getting this working, but I'm not sure the smooth damp is working at all. Have a look at this
var myVelocity : float = 0.0F;
var lookSmoothDamp : float = .3;
function Update () {
var finalAngle = Input.acceleration.x*40*-1;
var cameraRotation = $$anonymous$$athf.SmoothDamp(maincamera.transform.rotation.z, finalAngle, myVelocity, lookSmoothDamp);
if(finalAngle < 20 && finalAngle > -20) {
maincamera.transform.localEulerAngles.z = cameraRotation;
}
}
You can try put in your code inside LateUpdate, that will track your object more precisely
multiply finalAngle by Time.deltaTime and play with the number 90 (increase/decrease it to what you need)
Your answer
Follow this Question
Related Questions
Limit rotation from another script 0 Answers
How to create tilt control similar to temple run? 0 Answers
Why is the Gyroscope inverted? 0 Answers
Rotating a character smoothly 0 Answers