# How to always move a rigidbody with the same speed using a vector

My previous version of this question was unclear so I rewrote it.

I’ve been trying and trying but I cant wrap my head around this math/rigidbody thing:

What I want is to spawn a ball at the player, and then move it towards a target at an even speed no matter the distance between them. With the code I currently have, the ball gets closer and closer and slows down as it gets closer.

I have the correct vector3 to get to the target already. I’ve tried various things with normalizing the vector but this doesn’t help (I understood it would become 1 and then if I’d multiply by movementSpeed it would be fine but this doesn’t work). I know the direction to send it in by minusing the target’s and the player’s transforms. But now what? How do I tell the rigidbody “You must move exactly 1.0f per frame in the direction of this vector” (for example)

Somehow it should be a simple `Vector3 direction * movementSpeed * Time.DeltaTime`. But like I said, I can’t get my head wrapped around it.

Note that the target may be moving, thus the vector needs to be recalculated every frame to adjust to movement of the target.

Code so far (this is on the spawned prefab):

``````using UnityEngine;
using System.Collections;

public class MoveToTransform : MonoBehaviour
{
GameObject player;
PlayerSelection PS;
GameObject target;
Rigidbody rb;
public float desiredMovementSpeed = 2;
Vector3 direction;

void Start()
{
//Find target from script on the player
player = GameObject.FindWithTag ("Player");
PS = player.GetComponent<PlayerSelection>();
target = PS.selectedGO;

rb = GetComponent<Rigidbody> ();

calculateDirection ();
}

void FixedUpdate()
{
calculateDirection ();
rb.velocity =  direction /* * Time.deltaTime*/;
}

void calculateDirection()
{
direction = target.transform.position - this.transform.position;
Vector3.Normalize(direction);
}
}
``````

And then in the end I figured it out!

``````    void calculateDirection()
{
direction = target.transform.position - fireBall.transform.position;
direction = direction.normalized;
}
``````

and then it worked… Can’t believe it took me this long but oh well. (notice the difference in how I normalized, somehow this worked).

AddForce in FixedUpdate will add force to the rigidbody in every FixedUpdate frame. Thus it will create an effect of acceleration.

You are assigning the value of movementSpeed based on the distance. Greater the distance, greater is the force added hence greater is the speed.

If you want constant speed of the ball in the given direction. You can assign the velocity of its rigidbody a constant value in Start().

``````using UnityEngine;
using System.Collections;

public class MoveToTransform : MonoBehaviour
{
GameObject player;
PlayerSelection PS;
GameObject target;
Rigidbody rb;
public float desiredMovementSpeed = 10;
Vector3 direction;

void Start()
{
//Find player in scene
player = GameObject.FindWithTag ("Player");
//Find Script on player
PS = player.GetComponent<PlayerSelection>();
//Set selectedGO from that script to our local variable
target = PS.selectedGO;

//I use selectedGO to reference the player's target in another script.

rb = GetComponent<Rigidbody> ();

//Calculate movementspeed
calculateDirection ();

//Adds a constant velocity to the GO.
rb.velocity = direction * desiredMovementSpeed;
}

void calculateDirection()
{
direction = target.transform.position - this.transform.position;
}
}
``````