- Home /
Fire missile to mouse click
I simply wish to have an missile fire from an origin point to the location selected by the mouse. Currently, I am using Lerp, which works, but slows the missile down when it begins to reach the end point. I am also unable to identify when the missile reaches the end of it's movement.
missile.position = Vector2.Lerp(
missile.position,
des.transform.position,
Mathf.SmoothStep(0.0f,1.0f,speed));
speed+=Time.deltaTime*1.0f;
I'm sure that this has to be one of the simplest things to do in game creation(and I've done it a million times in other games) but Unity seems determined to strike me down. I can't seem to find a solid solution anywhere online either. So... help? :)
Hi, what if i what to fire missiles simultaneously at multiple mouse click positions?
Answer by syclamoth · Nov 13, 2011 at 04:30 AM
Your problem is that you're not interpolating between the start point and the end point, like you want to be- you're using a function which will smooth the missile's flight path as it goes- this will make the missile gradually approach des.transform.position, but never quite reach it. A more useful function for what you are doing here would go more like this-
// At missiles's launch (probably in Start, but you can put it wherever)
missileStartPoint = missile.position;
totalDistance = Vector3.Distance(missileStartPoint, des.transform.position);
distanceTravelled = 0;
curVelocity = missileVelocity;
// Each Update as the missile travels-
distanceTravelled += curVelocity * Time.deltaTime;
missile.position = Vector3.Lerp(
missileStartPoint,
des.transform.position,
distanceTravelled / totalDistance);
if(distanceTravelled / totalDistance >= 1)
{
// Missile destination reached! EXPLOADE (or whatever you missile does)
}
If you wanted your missile to start slow, and accelerate as it travelled, you could add a few lines like this in-
//when it starts
curVelocity = missileStartingVelocity;
// as it travels
curVelocity += missileAcceleration * Time.deltaTime;
Your answer
Follow this Question
Related Questions
Firing projectile towards mouse position from player... 0 Answers
How to make object clickable when user enter collider ? 0 Answers
How do you check if the object that you want to destroy with a click is the right object? 1 Answer
My inventory opens and closes on left mouse click when i don't want it to. 1 Answer
How Do You Exit A Game 0 Answers