# Immediately change Transform Position to desired constant speed

I have seen many posts related to this topic, one in particular but it was never answered. I have a “bullet” object on which a Camera focuses on. I would like to move the bullet to the speed I want immediately. I do not know the final position since it will stop on a Trigger. Here’s what I have so far.
The script will either move to a position or to a Trigger.

`

``````public class ObjectMover : MonoBehaviour
{
public bool useCollider = false;

// Stop at some position
public float stopX = 3.25f;
// Stop when you hit the collider with this tag
public string triggerName;

public bool forwardFirst = true;
public float speed = 3.5f;

bool stop = true;
float multiplier;
Vector3 originalPos;

private void Awake()
{
if (!forwardFirst)
{
multiplier = -1;
}
else
{
multiplier = 1;
}
originalPos = transform.position;
}

// Update is called once per frame
void Update()
{
if (!stop)
{
transform.position += multiplier * speed * transform.right * Time.deltaTime;
if (!useCollider)
{
if ((multiplier == 1 && transform.position.x >= originalPos.x) ||
(multiplier == -1 && transform.position.x <= stopX))
{
stop = true;
}
}
}
}

private void OnTriggerEnter(Collider other)
{
if (other.tag.Equals(triggerName))
{
other.enabled = false;
stop = true;
}
}

public void MoveTarget()
{
if (!stop)
{
stop = true;
}
else
{
multiplier *= -1;
stop = false;
}
}
``````

}
`

Thanks for any help

RigidBody Allows you to set the velocity of an object. Add a RigidBody Component and then use code like so:

``````    private Rigidbody rb; // Creates a RigidBody Component in the variable rb, used to control the Bullets Physics

void Awake()
{
rb = GetComponent<Rigidbody>();
}

public void SetVelocity() // Just an example func name
{
rb.velocity = new Vector3(20,rb.velocity.y,rb.velocity.z);
}
// sets the x velocity to 20, while leaving the Y and Z velocity exactly how they are
``````

You might need to mess with the RigidBody Component to stop gravity affecting the bullet etc, but this is what you’re looking for if you want to make an object have advanced physics