# How do I get something to move towards where an object WAS (Like a dodged bullet)?

I’ve been looking into the ways to move an object toward another. It works well when it’s a homing shot, such as this simple bit of code I mostly just copied from a Google-searched page:

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

public class Homing : MonoBehaviour {

// The target marker.
public Transform target;

// Speed in units per sec.
public float speed;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;

// Move our position a step closer to the target.
if (target != null)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, step);
}
}
}
``````

But it doesn’t work when it’s a bullet a player is supposed to dodge, since the bullet is only supposed to find the target’s position once. I ca’t really find much on this, so can someone help me figure out what needs to change to make this work?

Might be an idea to look into raycasts. Cast a ray from the origin of the gun toward the direction the bullet is being fired at and have it travel along that path.

You could calculate the angle using sine/costine/tangent and send projectiles that way, or just use Vector3.forward in relation to the player character’s position, or in the example below, in relation to the camera (assuming its an FPS)

``````public class Firearm : MonoBehaviour
{
private const float projectileDistance = 30.0f;

public void FireProjectile()
{
RaycastHit hit;

//cast a ray from the camera position, forward in the direction its facing until it hits an object
Ray ray = Camera.main.ScreenPointToRay ( Vector3.forward * projectileDistance );

//if the ray hit an object...
if (Physics.Raycast (ray, out hit, float.PositiveInfinity ) )
{
//store the hit point
Vector3 pointOfImpact = hit.point;

//if the hit object
if ( hit.transform.tag == "Enemy" )
{
enemy.life--;
}
}
}
}
``````

Of course if you don’t want the projectile to move instantly you could pass the hit.point to a newly instantiated bullet object, and in the bullets script have an update function that moves it in the direction of the hit.point.

``````public class Firearm : MonoBehaviour
{
private const float projectileDistance = 30.0f;

public void FireProjectile()
{
RaycastHit hit;

//cast a ray from the camera position, forward in the direction its facing until it hits an object
Ray ray = Camera.main.ScreenPointToRay ( Vector3.forward * projectileDistance );

//if the ray hit an object...
if (Physics.Raycast (ray, out hit, float.PositiveInfinity ) )
{
GameObject bulletObject = Instantiate ( bulletPrefab , playerCharacter.transform );

BulletScript bulletScript = bulletObject.GetComponent<BulletScript> ( );

bulletScript.SetTarget ( hit.point );
}
}
}

class BulletScript : MonoBehaviour
{
private Vector3 target;
public float projectileSpeed = 7.5f;

public void SetTarget( Vector3 target )
{
this.target = target;
}

private void Update()
{
transform.position = Vector3.MoveTowards ( transform.position , target , projectileSpeed );
}
}
``````

Thanks, although this isn’t an FPS. Does that make a difference? The bullet is meant to move only in the x and y coordinates, from an enemy to the player.

Sorta makes a difference, yeah. You can still use raycasts, just set the ray to start from the shooter instead of the camera.

I want to clarify something: It’s my fault that I used the word “bullet”, because right now there isn’t really a “gun”. There’s an object that spawns the projectile, but it doesn’t rotate. Thus notions of shooting “forward” don’t apply here.

Ideally, the projectile object itself would know which way to move as soon as it is spawned, based on where its target (the player, in this case) is. However, it needs to know where to go only once, so that by the time it has arrived at where the player was, the player might not be there anymore. Of course, for the sake of good mechanics the shot should keep going until it hits something else, but the point is that the player’s original position should indicate the direction in which the object is fired, but the trajectory itself should only be straight.

If the only, or just easiest way to do that, is with a rotating “gun” type object, please direct me to info on that, if you know any. I need to know how rotation works before I can really get anything from advice about raycasting.

I might be wrong but I think Forward is considered whichever direction the end point is. You could always do Vector2.Left or Right if I’m mistaken, and if you’re not sure you can use Debug.DrawRay to test its path.