- Home /
Shooting with new 2D tools top down single stick
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
}
}
The way you are doing your rotation is non-traditional and I'm not completely sure of the ramifications. Plus this is not top down to me since it appears the camera is facing positive 'z' rather than looking down on the 'y' axis. Here is a suggestion I make as a comment since I'm unsure of things:
var bulletInstance = Instantiate(bullet, GunPoint.position, transform.rotation); //instantiate the bullet at the correct position and set rotation to current rotation
bulletInstance.velocity = transform.right * bulletSpeed;
This code assumes that the front is on the right side of your sprite.
this would work if I only needed to shoot in one direction. I need to be able to shoot the way the player is facing so right wouldn't work.
Answer by KellyThomas · Dec 27, 2013 at 03:35 AM
Well you need to recall the direction from when input was last entered.
This would be one approach:
var facing: Vector3 = new Vector3(0.0f, 1.0f, 0.0f); //default to facing "up"
var deadzone: float = 0.1f;
//then in fixed update
if ( Mathf.Abs(PosH) > deadzone || Mathf.Abs(PosV) > deadzone) {
facing = new Vector3(PosH, PosV, 0.0f);
facing.Normalize();
}
if(Input.GetButtonDown("Fire1")){ //Press button to fire
var bulletInstance = Instantiate(bullet, GunPoint.position, Quaternion.Euler(facing)); //instantiate the bullet at the correct position and set rotation to current rotation
bulletInstance.velocity = facing * bulletspeed; //This is where the problem is cant figure it out
Destroy(bulletInstance, 1); //destroy the bullet
}
}
Your answer
Follow this Question
Related Questions
Fire Bullet To Mouse Position Problem 2 Answers
Rotate object based on normalized Vector2? 1 Answer
2 fingers sprite rotation 1 Answer
Rotate an object around another object at an angle from the X axis? 1 Answer
Vector rotation 1 Answer