Object moving and acceleration

Hi,

I’m trying to move an object from one place to another with the following script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movetarget : MonoBehaviour {
    public Transform[] target;
    public float speed;

    private int current;

    void Update() {
        if (transform.position != target[current].position)
        {
            Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
            GetComponent<Rigidbody>().MovePosition(pos);
        }
    }
}

It does work pretty well, my object is following a straight path to reach a target with a constant speed.

My problem is that when I push the start button, my object is moving forward directly with the maximum speed. I want it to reach this maximum speed but after a few seconds. I do not know how to make my object accelerate little by little to reach its maximum speed rather than start directly at maximum speed.

Can anyone help me ?

Thanks

You need to add some form of acceleration to it. Here’s a very simple example:

private int current;
        private float currentSpeed = 0;    // 0-1 from no speed to full speed
        private float accel = 0.5f;        // accelerate to full speed in 2 seconds (1 / accel)
    
        void Update() {
            if (transform.position != target[current].position)
            {
                currentSpeed += Mathf.Min(accel * Time.deltaTime, 1);    // limit to 1 for "full speed"
                Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * currentSpeed * Time.deltaTime);
                GetComponent<Rigidbody>().MovePosition(pos);
            }
        }

That’s the basic idea, but for nicer results you should use a vector to store your current speed towards the object. That’ll give your object some natural inertia which will also cause it to move in some nice curves if the target moves and your object needs to steer to keep moving towards it.

Hi,

Thanks for the answer. I’ve changed the code according to yours but it seems that my object doesn’t reach a maximum speed, it just continues to accelerate. I want my object to start with a speed of zero when I press play, accelerate for a few seconds to reach a maximum speed on which it will stay (let’s say 1.5). The goal is just to go from point A to a fixed point B automatically, I just want it to have an acceleration at the beginning to reach my max speed (no faster than someone walking) and stay on that speed for the rest of the travel.

Sorry my level in C# is bad… I’m a total beginner !

Thanks

It works fine ! Thank you very much :slight_smile:

Is it the solution that I deleted that is working? I deleted the post so that I could test it first and, yes, it is working. Here it is for the people who would want to know:

public Transform[] target;
    public float speed;
    public float currentSpeed;
    public float acceleration;

    private int current;

    void Update()
    {
        if (transform.position != target[current].position)
        {
            if (currentSpeed < speed)
            {
                currentSpeed += Mathf.Min(acceleration * Time.deltaTime, 1);
            }

            Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
            GetComponent<Rigidbody>().MovePosition(pos);
            Debug.Log(currentSpeed);
        }
    }

Yes it was for the previous one. Thanks for the complete code. And know, if I want my object to start moving after pressing the space bar on my keyboard, how do I do ?

I tried the following code but nothing happened after I pressed the space bar on my keyboard…

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (transform.position != target[current].position)
            {
                if (currentSpeed < fullSpeed)
                {
                    currentSpeed += Mathf.Min(accel * Time.deltaTime, 1);    // limit to 1 for "full speed"
                }
                Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * currentSpeed * Time.deltaTime);
                GetComponent<Rigidbody>().MovePosition(pos);
            }
        }
    }

I want the object to move when I press the key once and continue even after releasing the key. I just want to trigger the event by pressing once.
As I said, I’m still a newbie on Unity and I’m trying to learn C# but it’s not easy…
It will be my last question, thank you for your time, it’s already a big help !

Yep, you got the wrong code:

Try:

        if (Input.GetKey(KeyCode.Keypad1))
        {
            if (transform.position != target[current].position)
            {
                if (currentSpeed < fullSpeed)
                {
                    currentSpeed += Mathf.Min(accel * Time.deltaTime, 1);    // limit to 1 for "full speed"
                }
                Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * currentSpeed * Time.deltaTime);
                GetComponent<Rigidbody>().MovePosition(pos);
            }
        }

Nice ! But when I release the key, my object stops. But it works perfectly when I hold down the button. I want the object to continue moving even after I release the key. I want the key to be a simple trigger that says to my object when I push and release it: “Ok, now you can move”.

In that case, use:

if (Input.GetKeyDown(KeyCode.Keypad1))

:slight_smile:

@Alexis1956 This is more precise though, the speed will stabilise at the exact level specified:

        if (Input.GetKeyDown(KeyCode.Keypad1))
        {
            if (transform.position != target[current].position)
            {
                if (currentSpeed < fullSpeed)
                {
                    currentSpeed += Mathf.Min(accel * Time.deltaTime, 1);    // limit to 1 for "full speed"
                }
                else
                {
                     currentSpeed = fullSpeed;
                }

                Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * currentSpeed * Time.deltaTime);
                GetComponent<Rigidbody>().MovePosition(pos);
            }
        }