Vector3 acceleration

Who can help me with this script? I add this script to my character, but it has to run faster. This is what I have:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	rigidbody.velocity=new Vector3(6,0,0);
	
	}
}

Now I need a line of code that adds every 5 seconds 1 to the Vector3 x axis, so it accelerates and the object moves faster.
Who can help me to complete this code?

acceleration is not velocity… acceleration adds to velocity.

Usually acceleration is defined as units per second per second, or units/sec^2

You want 1 unit in the x direction, <1,0,0> added every five seconds. That’d be <1,0,0> / 5, or <0.2,0,0> added every 1 second.

Ok, so we need to ad <0.2,0.0> units/sec every duration of a second in the update function. But the update function doesn’t update every second, it updates every fraction of a second. That change in time is stored in ‘Time.deltaTime’. So just like we got the acceleration for 1 second just by division, we can get this fractional just by scaling. And we used the 1 second as the base because it’s easy to understand, and it scales nicely due to the identity theorem.

<0.2,0,0> * Time.deltaTime

we end up with:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

	public Vector3 Acceleration = new Vector3(0.2f, 0.0f, 0.0f);

    // Update is called once per frame
    void Update ()
	{
		rigidbody.velocity += Acceleration * Time.deltaTime;
    }
}

note, this will add infinitely… you may want to apply some terminal velocity (the max velocity it could ever be)

//up by Acceleration
float TerminalSpeed = 20.0f;


//in update
Vector3 vel = rigidbody.velocity + Acceleration * Time.deltaTime;
if(vel.magnitude > TerminalSpeed) vel = vel.normalized * TerminalSpeed;
rigidbody.velocity = vel;

Hey thanks a lot for the script! Hope it can help other people too.

How would I use this script if it’s on a character controller instead of a rigidbody?

A CharacterController has a ‘Move’ method and you pass in the amount you want to move that frame as a Vector3. That amount would be the ‘Velocity * Time.deltaTime’ where ‘Velocity’ is your expected Velocity right now.

This implies you need a place to store the current velocity. You may think you want to use the ‘velocity’ property of the CharacterController but unfortunately that value is not always valid. As it says in the documentation:

So really you should be storing the velocity locally.

Something like this:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    public CharacterController Controller;
    public Vector3 Acceleration = new Vector3(0.2f, 0.0f, 0.0f);
   
    private Vector3 _velocity = Vector3.zero;

    // Update is called once per frame
    void Update ()
    {
        _velocity += Acceleration * Time.deltaTime;
        Controller.Move(_velocity * Time.deltaTime);
        _velocity = Controller.velocity; //resync the velocity incase we hit a wall
    }
}