- Home /
Fire/Shoot at intersect point
Ok so i know this has been answered before and i know its simple game math and i thought i had the right function (FromToRotation) but it just keeps going in one direction and i cant figure out what math im looking for, so i apologize
I want to fire a projectile from my player to wherever on the ground i clicked. I am able to Instantiate the projectile and make it move in a fixed direction but i cannot seem to figure out how to angle the projectile to face the intersect point on the ground and move towards it. I know where the intersect point is as well, just not the math to do the rest
if(Input.GetMouseButtonDown(0)){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
var didHit : boolean = Physics.Raycast(ray, hit);
//var cursor = GameObject.Find(currentCursor.name);
if(didHit) {
var angle = Quaternion.FromToRotation(shootFrom.transform.position, hit.point);
var spell = Instantiate(prefab, shootFrom.transform.position, angle);
spell.rigidbody.AddForce(transform.right * shootSpeed);
}
}
I tried to determine the angle but it doesn't seem to work. What am i doing wrong?
Answer by syclamoth · Dec 01, 2011 at 05:03 AM
You're looking for Quaternion.LookRotation
As in,
var angle = Quaternion.LookRotation(hit.point - shootFrom.transform.position);
Seriously, the script reference is your friend!
Edit: Scratch this, i had something somewhere else affecting it. Thanks!
Thanks for your input! I had actually used that originally but the projectile still fired along the x axis ins$$anonymous$$d of where i clicked so i thought it was the wrong one and used FromToRotation. Is there something else i need to do with angle before i put it in the instantiate method?
Do you want it clamped in one axis? Or do you want it to point directly at where you clicked? If you want it clamped in one axis, all you need to do is change the 'hit.point' bit-
var correctedPosition = new Vector3(0, hit.point.y, hit.point.z);
// Zero out a different value to clamp a different axis!
Then use correctedPosition, ins$$anonymous$$d of hit.point!
Nope, i just wanted to shoot towards wherever i clicked. However.. $$anonymous$$y guy fires in a direction when i click on the ground but its off a little and not sure what to do to fix it. When i use the LookRotation by itself, my projectile barely moves so i attempted to apply a speed to it by multiplying the angle by a vector3
var newAngle = angle * new Vector3(1000, 1, 1000);
Screenshot here - http://enspired.us/example.png
I clicked where my mouse is but as you can see it is off a little. Ideas? I know it has to do with my math but i wasnt sure how else to apply it
Oh, if the shot is a rigidbody then there's no way of preventing it from falling down over time- this happens because of gravity! I think what you really need here is trajectory analysis- which is actually a bit more complicated.
Heh thats what i was afraid of. Ok then, guess its time to start studying up! Thanks!! =-)
Your answer
