- Home /
Top down 2d shooting - XZ rotation
I'm trying to shoot a projectile in the direction of the mouse cursor, but for some reason with my current code I can't get it to move in the Z direction. It will only follow the mouse on the X plane, the degree of Z rotation seems to just modify the speed at which the projectile moves, I used the code below from the answer by aldonaletto in this question to help me, but I don't fully understand this part:
transform.Rotate(0,Input.GetAxis("Horizontal")*60*Time.deltaTime,0);
transform.Translate(0,0,Input.GetAxis("Vertical")*10*Time.deltaTime);
Why 60 and 10? and why rotate horizontally but translate vertically? I tried removing this code and messing around with it to see what it does but it doesn't seem to affect the projectile in any way. I use orthello 2d, could that be affecting it?
Here's the full code from the answer I'm talking about - it's what I'm using to shoot a projectile.
var bullet:GameObject;
var BulletSpeed:float = 20;
function BulletShot(){
var myPos = transform.position;
var dist = Camera.main.transform.position.y-myPos.y;
var x = Input.mousePosition.x;
var y = Input.mousePosition.y;
var dir = Camera.main.ScreenToWorldPoint(Vector3(x, y, dist))-myPos;
var BulletClone = Instantiate(bullet, myPos, Quaternion.LookRotation(dir));
BulletClone.rigidbody.velocity = dir.normalized * BulletSpeed;
Physics.IgnoreCollision(BulletClone.collider, collider);
Destroy(BulletClone,5);
}
function Update(){
transform.Rotate(0,Input.GetAxis("Horizontal")*60*Time.deltaTime,0);
transform.Translate(0,0,Input.GetAxis("Vertical")*10*Time.deltaTime);
if (Input.GetButtonDown("Fire1")){
BulletShot();
}
}
Any help would be very appreciated :)
Rotation is done around an axis. $$anonymous$$eaning rotation on the Y axis, rotates it left and right.
Translation is done on an axis. $$anonymous$$eaning translating on the Z axis translates it forward and back.