Hey, I’m having trouble moving a missile in my game to it’s target position at a constant speed regardless of distance.
With my current solution the missile moves towards the target but it takes the same time to get to a close location as it does to get to a location really far away. For some reason the missile slows down and speeds up based on the distance it needs to travel?
public class MissileScript : MonoBehaviour
{
public Vector3 target;
public Vector2 spawnPos;
public GameObject explosionAnim;
public float speed;
public float explosionTime;
public Transform explosionPos;
public AudioClip explosionAudio;
Vector3 direction;
// Start is called before the first frame update
void Start()
{
transform.position = spawnPos;
direction = (target - transform.position).normalized;
}
// Update is called once per frame
void Update()
{
rotateToFaceMouse();
//move the missile
if(Vector3.Distance(target, transform.position) <= 0.2)
{
explode();
}
else
{
transform.position = transform.position + direction * speed * Time.deltaTime;
}
//transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
/// <summary>
/// Rotate sprite to face mouse
/// </summary>
void rotateToFaceMouse()
{
Vector3 vectorToTarget = target - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg - 90;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, 1);
}
/// <summary>
/// Display animation for explosion and delete missile game object
/// </summary>
void explode()
{
AudioSource.PlayClipAtPoint(explosionAudio, new Vector3(0, 0, 0));
explosionAnim.GetComponent<ExplosionScript>().explosionTime = explosionTime;
explosionAnim.GetComponent<ExplosionScript>().spawnPos = transform.position;
Instantiate(explosionAnim, new Vector3(0,0,0), Quaternion.identity);
Destroy(this.gameObject);
}
}
As you can see I’ve tried using Vector3.MoveTowards as well as just changing the position. I’m completely stumped… any help would be really appreciated!
To clarify what upjohn wrote, both of the things you have there are fine. Either one should be moving you at a constant speed. So there must be something else wrong in what we can’t see – possibly “speed” slowing down or speeding up when it shouldn’t.
When I saw the Q, I assumed you used Lerp by mistake, since that tends to give funny speeds.
Speed has a constant value that I’ve entered in through unity.
Below is the code that instantiates the missile
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MissileControllerScript : MonoBehaviour
{
public GameObject missile;
public Vector2 spawnPos;
public float speed;
public KeyCode fireButton;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(fireButton))
{
missile.GetComponent<MissileScript>().spawnPos = spawnPos;
missile.GetComponent<MissileScript>().target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
missile.GetComponent<MissileScript>().speed = speed;
Instantiate(missile);
}
}
}
Check the missile’s speed variable as it runs (Pause can help here). Set up some with dummy targets if it helps. Possibly create a new scene with only a pre-made missile. Make sure it really is moving at different speeds – maybe save the last position, and print the difference after each move. Print old/new position at each step and check old == previous new (or else someone else is moving your missiles). Test fire them going left/right/up/down to see if there’s some other pattern to the wrongness.
I’ve checked the speed variable… it stays at 20 and doesn’t change. The missiles are definitely moving at different speeds, the behavior is working almost identical to lerp.
It looks like the missiles have a time to arrive at their target so if the target is close, it slows down so it makes it on time and if it’s far it speeds up to reach the target on time. Although oddly enough, I did some testing and it seems like the missiles top speed is capped… So if the target is extremely far away, it only goes up to a certain speed and doesn’t go any faster.
Since this looks like a 2d game ( using sprites ) check if they are spawned at the same height in 3d space.
It can look like the sprite is moving slower in 2d if the sprite is far away in the not shown dimension.