- Home /
Adding curve to Vector3.MoveTowards
So for some projectiles I have set up they are using MoveTowards to attack an enemy target.
I would really like to have the projectile curve out the side before tracking down the target.
Earlier I was using the distance to the target to alter the movement of the projectile, meaning the curve would become less and less the closer the object got to the target(which is what I wanted).
This ended up working to some extent, but the curve value would end up slowing down the projectile, whenever it was moving at a certain direction relative to the world coordinates.
It went something like this...
var distanceToTarget = Vector3.Distance(myTarget.position, transform.position);
transform.LookAt(myTarget);
transform.position = Vector3.MoveTowards(transform.position, myTarget.position, Time.deltaTime * latchSpeed);
transform.position.x += distanceToTarget / 100;
I figured since the object was always facing towards the object, I wouldn't run into issues with just adding a value to the x position. However it acted weird in different quadrants of space, as if it were being used with World.Space. I tried using transform.localPosition, but the results were the same.
any suggestions? this is a pretty bad hack job, I am sure there is a much cleaner solution.
I was thinking of adding a Vector3.right value to it, see if that yields any results.
Answer by sparkzbarca · Nov 15, 2012 at 08:22 AM
thank you for the response, it looks promising. I have avoided trying to use splines as I am unsure how they will react when the end point is a moving object. I also fear the computations will be excessive when there is potentially hundreds of these on the screen.
I will run a trial setup and see if I can get it to function how i would like.