i have a 2D game, using the x,y axes to move around the screen and a player that shoots bullets torwards the screen.
i got different scripts for the missile script, either addForce to the bullet or make the position of the bullet go towards the mouse
Now how to i make it so i shoot at an angle like instead of shooting one bullet directly towards the mouse when i level up i shoot instantiate 2 bullets at a fixed angle away from the mouse position?
thx u
//Calcualtes where the mouse is on the scren
var mousePos : Vector3 = Input.mousePosition;
mousePos.z =- (transform.position.z - Camera.mainCamera.transform.position.z - 14);
var worldPos : Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);
var targetDelta = (worldPos - transform.position);
//Shoot torwards mouse
var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, Quaternion.identity);
var targetDelta = (worldPos - transform.position);
var launchVelocity = targetDelta.normalized * MissileSpeed;
Missile.velocity = launchVelocity;
OR this is another way of shooting towards the mouse if u prefer that way
var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, transform.rotation);
Missile.rigidbody.AddForce(targetDelta * 300);
EDIT
This is the new code which DOES shoot at an angle but the closer the mouse pointer the slower the bullet speed, i think i had this problem b4 but didnt notice it somehow O_x
i get its because the mouse position changes and im multiplying it to the distance of the mouse position, just dunno how to fix that.
var eulerAngles : Vector3;
in UPDATE Function()
//Calcualtes where the mouse is on the screen
var mousePos : Vector3 = Input.mousePosition;
mousePos.z =- (transform.position.z - Camera.mainCamera.transform.position.z - 14);
var worldPos : Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);
EDIT again(my bad i put worldPos instead of targetDelta here which was wrong)
EDIT yet AGAIN
cause I FIXED IT!! woot!
used targetDelta.normalized and it works, which is surprising since im dont rly know wat it does and it was my 1st guess which i didnt even know where i should have started xDDDD
var force : Vector3 = (targetDelta.normalized * 800);
//Instantiate one bullet 20 degrees right of the mouse
eulerAngles.z = -20;
var newForce : Vector3 = Quaternion.Euler(eulerAngles) * force;
var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, transform.rotation);
Missile.rigidbody.AddForce(newForce);
//Instantiate one bullet 20 degrees left of the mouse
eulerAngles.z = 20;
newForce = Quaternion.Euler(eulerAngles) * force;
Missile = Instantiate(MissilePrefab, transform.position, transform.rotation);
Missile.rigidbody.AddForce(newForce);