1
\$\begingroup\$

I have an orthographic camera set on position(0,0,-10), rotation(0,0,0) and it's parent - CameraHolder with a transform:

enter image description here

which gives an isometric feel: enter image description here

Now, I need to move the ball with the accelerometer so here's how the FixedUpdate() looks like:

   void FixedUpdate ()
     {
         if (SystemInfo.deviceType == DeviceType.Desktop) 
         { 
             // Player movement in desktop devices
             // Definition of force vector X and Y components
             float moveHorizontal = Input.GetAxis("Horizontal");
             float moveVertical = Input.GetAxis("Vertical");


             // Building of force vector
             Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
             // Adding force to rigidbody
             rigidbody.AddForce(movement * speed * Time.deltaTime);
         }
         else
         {
             // Player movement in mobile devices
             // Building of force vector 
             Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
             // Adding force to rigidbody
             rigidbody.AddForce(movement * speed * Time.deltaTime);
         }
     }

The movement looks like this: enter image description here

And I want it to move like: enter image description here

I need to translate the movement by 45 degrees, but I have no idea how.

If anyone has a clue, I'd be grateful for help!

\$\endgroup\$
1
1
\$\begingroup\$

If the movement will always be camera locked, you can compute the movement vectors using the camera transform.

You would use this whenever the camera rotates

//Take care to normalize AFTER setting y to 0
Vector3 camFwd = cam.transform.forward;
camFwd.y = 0; camFwd = camFwd.normalized;

Vector3 camRight = cam.transform.right;
camRight.y = 0; camRight = camRight.normalized;

and you would use this whenever movement should occur

Vector3 acc = Input.acceleration;
Vector3 movmenet = new Vector3(camRight * acc.x,0.0f,camFwd * acc.z);

You could also use a Quaternion / Matrix and rotate the vector, but that's way more costly and complicated than necessary.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.