0

I wish to rotate my object in the direction it is travelling. How can I calculate the Vector difference from the previous frame and current frame and hence give a direction to aim towards to make it rotate in the direction it is travelling. The following is the code being used to control the object by accelerometer.

Vector3 getAccelerometer(Vector3 accelerator){

    Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
    return accel;
}


void Start(){
    calibrateAccelerometer();
}

void Update(){
    Vector3 dir = getAccelerometer(Input.acceleration);
    if (dir.sqrMagnitude > 1)
        dir.Normalize();
    transform.Translate(dir * speed * Time.deltaTime);
}
Bart
  • 19,692
  • 7
  • 68
  • 77
dhully
  • 1
  • 3
  • Sounds strange. You already have `Vector3 dir` which is a direction in which you move your object. Why don't you want to use it? – Sergey Krusch May 17 '14 at 10:16
  • float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle-90.0f, Vector3.forward); The above is the original code that was working for me when I was using the mouse to move my object around by simple moving the mouse. The object would turn towards it and start moving towards it.Im trying to apply the same code here but after adding this code,the accelerometer starts acting all weird and the object doesnt travel in the direction of the tilt anymore,nor does the rotation of the object work correctly and it just randomly rotates. – dhully May 17 '14 at 15:45
  • I see. Well, unfortunately, I'm not an expert in accelerometers, gyroscopes, etc. Try to edit your question. Explain what exactly you want to achieve. Because what you ask now if far from what you have explained in your comment. – Sergey Krusch May 17 '14 at 16:01
  • I did not add that code so as to not confuse the reader with a non-working code. I guess the question stands fine, and what kind of code do I need to add for the rotation to work fine. Thanks for the help Sergey. – dhully May 17 '14 at 16:16
  • Ok. Here is the list of questions that flash into my mind: 1. How exactly you want your object to be controller by phone (or tablet) movement? 2. What kind of game are you trying to create? 3. Are you sure that you need accelerometer, not gyroscope? 4. What does calibrateAccelerometer method do? How calibrationMatrix is constructed? Answering them will help other people understand what you want. This means that it will help you to get their help :) – Sergey Krusch May 17 '14 at 16:32
  • 1. Tilting the phone/tablet left makes it move left, and likewise for any direction. 2. A 2D top down game 3. I don't know the difference between the two, though accelerometer seems to be working fine for me. 4. calibrateAccelerometer checks for the angle of the device on the game start and keeps that value as the flat angle so the game can be played in a more comfortable manner since having the device flat to play it at all times is not fun. Hope this helps in understanding my problem better. – dhully May 17 '14 at 17:03

0 Answers0