- Home /
Question by
noobunity · Oct 02, 2014 at 10:21 AM ·
shootingmousepositionscreentoworldpointfromtorotation
Need explanation for this code
Hi all
Following is the code to fire bullet in the direction of mouse for a 2D platformer.(thanks Robertbu for the answer)
Can somebody please explain me the code from pos.z to var q? those three lines in below code? what exactly its doing
var prefab : GameObject;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
var go = Instantiate(prefab, transform.position, q);
go.rigidbody.AddForce(go.transform.up * 500.0);
}
}
Thanks
Comment