- Home /
Code for rotating in direction of motion with the mouse not working when switched to accelerometer.
HERE is my previously answered question to achieve the rotation when the mouse is in question. I tried to convert the same code to control the object using the Accelerometer, but neither does the object move in the direction of the tilt, nor is it rotating in that direction. Here is my current code for the accelerometer.
void Update(){
Vector3 dir = Input.acceleration;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle+90.0f, Vector3.forward);
}
If anyone else has any suggestions for this, I request you to kindly add them. Thanks !
@fafase, I've been after this issue for quite a few days and have tried a lot of solutions, some from other questions, forums and some my own. I also don't encourage the blatant copy-and-pasting nature that can easily become a part of a newcomers' (like me) Unity learning process. Every question that I've asked here has been asked only after I tried and gave my best before asking for help. If that is the reason you feel that I deserve a downvote, I wish I could do more to not deserve it. As for asking for robertbu to be the one to ask this question to specifically, as I explained that he may be the best person to answer this as he had helped in pretty much the exact question in the past. Further apologies if I may have harmed the integrity of this wonderful forum, though such was not my intention to do so.
It is not prohibited to call out a particular person, but it bad form and you will get some down votes. In addition I cannot currently help you. It is been ages since I played with the acelerometer, and I'm away from by device built setup. If I cannot visualize the acelerometer outputs, I cannot write a solution. So I suggest you edit your title and allow @fafase (or others) to help you. @fafase gives solid solutions.
@robertbu I have edited the title and the question itself as suggested by you. I may have committed a social goof up and hope the community here accepts my mistake. I request anyone with any pointers to help me out with my situation here. Thanks !
Answer by fafase · May 18, 2014 at 03:45 PM
I would think the problem comes from the fact that the accelerometer uses a different coordinate system
If your world is flat, then the y axis of the accelerometer is the forward (z) axis of your object.
Here is how we were doing for a game using the accelerometer to move a mouse toy object:
void Update () {
Vector3 planarVelocity;
rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,0,Input.acceleration.y*speed);
planarVelocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
if (planarVelocity.magnitude > 0.5f)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(planarVelocity), rotSpeed*Time.deltaTime);
}
}
There are a few variables you need to create and define but else it is quite straightforward approach.
The acceleration is used to define the velocity of the rigidbody, this, compares to Translate will creates a movement that interacts with the environment while Transalte will get you through any collider.
Then planarVelocity is just a fancy word, our programmer found to describe the direction vector...Then it is just a basic Slerp of the transform rotation.
Thanks for replying, fafase. Also, thanks for explaining the code. $$anonymous$$y project is a top down game with the object facing Up by default and also as the forward axis of the object. On using your code, I'm getting errors in Unity that say that there is no Rigidbody attached to the object or the script needs to check if the component is attached before using it. Oddly, I do have the Rigidbody component attached. Though I have the $$anonymous$$ass = 1 and Gravity Scale = 0. Having Gravity Scale = 9.8 makes the object fall down. Neither FixedAngle nor Is$$anonymous$$inematic is ticked on. Interpolate is set to None.
EDIT: I have a question. Since we are using the X-Y plane, why are you not using Input.acceleration.y*speed in the Y coordinate of the rigidbody.velocity vector ? Similarly, why have rigidbody.velocity.y = 0 and have rigidbody.velocity.z to have a value, when we basically have nothing to do with the Z depth ?
EDIT: I was getting that error because I was using Rigidbody2D. I changed it to to use Rigidbody and the code worked in Unity but the object was just travelling along the X axis and flipping in the Y axis. I made some edits to the code and the following is where I reached.
Vector3 planarVelocity;
rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,Input.acceleration.y*speed,0);
planarVelocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z);
if (planarVelocity.magnitude > 0.5f)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(planarVelocity), rotSpeed*Time.deltaTime);
transform.rotation = Quaternion.Euler(lockPos, lockPos, transform.rotation.eulerAngles.z);
I had to add the last line since I had to lock the rotations to 0 in the X and Y axis and only have Z rotations since they are perpendicular to the screen and don't skew as X and Y rotations do. But, the object still does not face the direction it is moving in. There are rotations happening but they are not in the direction the object moves.
Concerning the last line you can constrain the rigidbody via the inspector.
As for why the y axis in the z position, I am myself a little confused why our programmer went this way, though it worked when device is used with home button to the right.
Also, if you need more Input info, look there:
http://docs.unity3d.com/Documentation/$$anonymous$$anual/Input.html
Constraining the X and Y rotations of the rigidbody via the inspector is making the object rotate in exactly those axes, which is very weird. I'll just stick to manually locking the rotation as it is in my previous comment. As for the rotation goes, the Input manual didn't help as I can't be sure what other workaround can work for this. Just so it is more clear, the kind of motion that I'm going for is pretty much like in this gameplay video - HERE
Yay! I got it to work finally. I had to mix up your code and the previous code for it to face in the direction it is travelling in. I also managed to get the constraining to work on the X and Y axes after using this code so that is a win-win for us. Here is the working code:
Vector3 planarVelocity;
rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,Input.acceleration.y*speed,0);
planarVelocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z);
float angle = $$anonymous$$athf.Atan2(rigidbody.velocity.y, rigidbody.velocity.x) * $$anonymous$$athf.Rad2Deg;
if (planarVelocity.magnitude > 0.5f)
transform.rotation = Quaternion.AngleAxis(angle(add offset if needed here), Vector3.forward);
Your answer
Follow this Question
Related Questions
accelerometer Rotation limiting 1 Answer
Adding animation clip stopped my gameobject from rotating. 1 Answer
Manipulate an objects rotation 1 Answer
Sphere "skids" on turns without rotating 1 Answer
Smooth tilt accelerometer 1 Answer