- Home /
Using Accelerometer to move and rotate a 3d cube from left to right
I have been working on making a simple dodge the falling cube game and I have my movement script working, the problem that I'm having is that I want my cube (Player) to rotate on its z axis in the direction that it moves when it moves (ex: phone tilts left - cube moves left and rotates left on z axis). I would also like to know if anyone knew of a way that I could make the tilt either more or less sensitive. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour {
Rigidbody rb;
float dirX;
public float moveSpeed = 20f;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
dirX = Input.acceleration.x * moveSpeed;
transform.position = new Vector2 (Mathf.Clamp (transform.position.x, -7.5f, 7.5f), transform.position.y);
}
void FixedUpdate()
{
rb.velocity = new Vector2 (dirX, 0f);
}
}
That's my code it does work in moving my cube and rotating it on the z axis, but I want it to be a lot more smooth with the rotation. An example of the kind of movement I need in case I'm bad at explaining things, a good example would be in Falling Blocks. Thanks in advance.
Your answer
Follow this Question
Related Questions
left/right rotation force 0 Answers
android accelerometer left right 3 Answers
Unity Realistic accelerometer controls 1 Answer