Moving an object by Force

There are many ways to move an object. Today, we will look into on how to move an object that has Rigidbody component attached by using AddForce method.
Create a Plane and a Sphere. Add a Rigidbody component to the sphere, create and attach a script to the sphere as well.

    Rigidbody rb;
    float speed;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        speed = 5f;
    }

    void FixedUpdate()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveY = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveX, 0.0f, moveY);
        rb.AddForce(movement * speed);
    }

Dodaj komentarz

Twój adres email nie zostanie opublikowany.