I remove indications for body movement, depending on the inclination of the device, the code took off. Input Manual

public float speed; Vector3 dir; // Use this for initialization void Start () { speed = 10.0f; dir = Vector3.zero; } // Update is called once per frame void FixedUpdate () { // пробовал и просто с Update, вроде с физикой лучше использовать Fixed dir.x = Input.acceleration.x; dir.z = Input.acceleration.y; // clamp acceleration vector to the unit sphere //if (dir.sqrMagnitude > 1) //dir.Normalize(); // Make it move 10 meters per second instead of 10 meters per frame... dir *= Time.deltaTime; // Move object transform.Translate(dir * speed); } 

Everything works, and it is excellent and responsive, but sometimes, when the bodies touch each other (for example, the player touches the walls and slides along them for a while) something goes wrong and one of the axes is inverted, or starts to move vertically! Jumps player on which the script.

    1 answer 1

    I recommend instead of using transform.Translate() use rigidbody.AddForce()

    It is necessary in Start to get the Rigidbody component using the GetComponent<>() method and use this component to move the player.

    Demo code:

     using UnityEngine; [RequireComponent(typeof(Collider))] [RequireComponent(typeof(Rigidbody))] public class AccelerationTest : MonoBehaviour { public float speed = 10f; private new Rigidbody rigidbody; private Vector3 dir = Vector3.zero; private void Start () { rigidbody = GetComponent<Rigidbody>() } private void FixedUpdate () { dir.x = Input.acceleration.x; dir.z = Input.acceleration.y; rigidbody.AddForce(dir * speed, ForceMode.Force); } } 
    • I don’t know what the joke is, but it doesn’t even move = (The code rewrote directly exactly. The script is attached to the object. Rigidbodie hangs on the object. What could be the matter? - Dmitrii
    • All is well. Wrong in a pair of characters. Works - Dmitrii