|
This is my bullet code: function Awake () { target = GameObject.FindWithTag("Enemy"); xPosition = target.GetComponent("Transform").transform.position.x; zPosition = target.GetComponent("Transform").transform.position.z; xDifference = Mathf.Abs(transform.position.x - xPosition); zDifference = Mathf.Abs(transform.position.z - zPosition); } function Update ()
{ function Go () { if (xDifference < .01) x = 0; else { if (transform.position.x < xPosition) { x = speed * Time.deltaTime; } if (transform.position.x > xPosition) { x = -1 * speed * Time.deltaTime; } }
} (well, there's more to it, but that's the condensed version) I want the bullet I fire to go in a straight line, but if the object in question isn't an equal distance away in x and z, the bullet unrealistically turns in mid air. Can I please have help?
(comments are locked)
|
|
The problem is that you only have one speed for both x and z. What is happening is that it goes at a 45 degree angle until either xdifference or zdifference is zero, and then goes straight to the object.
What you need to do is make two speeds (one for x and one for z) and them calculate them based on the angle between where the bullet starts and its target, possibly by using the vector between the two points.
(comments are locked)
|
|
Or you can do this vector math:
and tada the bullet will go straight forward from where you shoot it. also you can change transform.forward for any direction vector which can be something like this:
hope its usefull. :P thanks (moar letters)
Jul 21 '10 at 06:36 PM
The BOOM
(comments are locked)
|


just out of curiosity and to confirm before posting some answer: is there a reason why you don't simply apply a force to your bullet object? If the force is large enough, the path will be very close to a straight line.
no reason actually, and with no gravity set it could be a straight line, thnx for the suggestion