[19776-screen+shot+2013-12-26+at+9.04.42+pm.png|19776]
The player uses the left joystick of a controller (Horizontal and Vertical axis so it worked with WASD) to move and the player faces the direction of the moment. I made it so you shoot when pressing (“Fire1”) and it only works when you are moving because it gets the axis of the movement of the joystick. This is a problem because I need you to still be able to shoot when you are standing still. The bullet also needs to go at a steady speed. The z rotation is the correct degree the bullet needs to go in but I don’t know how to make that work . Take a look at what I have so far. (Using new 2D toolset)
#pragma strict
var speed : float; //speed of the player
var PosH : float; //value of change in the Horizontal axis of the controller
var PosV : float; //value of change in the Vertical axis of the controller
var bullet : Rigidbody2D; //Projectile being shot has a Rigidbody2D component
var GunPoint : Transform; //Point at the tip of the gun where the bullet is instantiated
var bulletspeed : float; //Constant speed the bullet goes
function FixedUpdate(){ //FixedUpdate
PosH = (Input.GetAxis("Horizontal")); //get the float of change of the axis
PosV = (Input.GetAxis("Vertical")); //get the float of change of the axis
gameObject.transform.position.x = gameObject.transform.position.x + speed * PosH; //move the player baced on the speed and change in the axis
gameObject.transform.position.y = gameObject.transform.position.y + speed * PosV; //move the player baced on the speed and change in the axis
gameObject.transform.LookAt(transform.position + Vector3(PosH, PosV, 0.0), Vector3.forward); //rotate the player on the z axis baced on the vector of the joystick
gameObject.transform.rotation.y = 0; //lock the player rotation to stay 2D
gameObject.transform.rotation.x = 0; //lock the player rotation to stay 2D
if(Input.GetButtonDown("Fire1")){ //Press button to fire
var bulletInstance = Instantiate(bullet, GunPoint.position, Quaternion.Euler(Vector3(PosH,PosV,0))); //instantiate the bullet at the correct position and set rotation to current rotation
bulletInstance.velocity = Vector2(bulletspeed * PosH, bulletspeed * PosV); //This is where the problem is cant figure it out
Destroy(bulletInstance, 1); //destroy the bullet
}
}