- Home /
2D Shooting right only. Doesn't flip direction with character
Hi,
My situation is that I have a 2D platformer. The shooting works, but only towards the right direction. I have attached the script to a empty game object to position the shooting position exactly to where I want. I have tried several ways; trying to add another direction and trying to flip the child, all which I couldn't do.
I have 3 different scripts that I use to do this...
My First:
public class InfitityMovement : MonoBehaviour {
public Vector2 speed = new Vector2(10, 10);
public Vector2 direction = new Vector2(-1, 0);
private Vector2 movement;
// Update is called once per frame
void Update () {
movement = new Vector2(
speed.x * direction.x,
speed.y * direction.y);
}
void FixedUpdate()
{
// Apply movement to the rigidbody
rigidbody2D.velocity = movement;
}
}
The Second:
public class PlayerShooting : MonoBehaviour {
public Transform bullet;
public float shootingRate = 0.25f;
private float shootCooldown;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
public void Attack(bool isEnemy)
{
if (CanAttack)
{
shootCooldown = shootingRate;
// Create a new shot
var shotTransform = Instantiate(bullet) as Transform;
// Assign position
shotTransform.position = transform.position;
// The is enemy property
ShootingControl shot = shotTransform.gameObject.GetComponent<ShootingControl>();
if (shot != null)
{
shot.isEnemyShot = isEnemy;
}
// Make the weapon shot always towards it
InfitityMovement move = shotTransform.gameObject.GetComponent<InfitityMovement>();
if (move != null)
{
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
}
}
public bool CanAttack
{
get
{
return shootCooldown <= 0f;
}
}
}
Lastly...
public class ShootingControl : MonoBehaviour {
public int damage = 1;
public bool isEnemyShot = false;
// Use this for initialization
void Start () {
Destroy (gameObject, 10);
}
}
I have tried playing around with the transform.right section, didn't work. I tried changing the Vector2 direction variable to different numbers, didn't work. Ive just been clueless all day!
Side note, my character flips to direction using localScale
What I don't have to answer your question is how you want to aim. Do you want to aim at a target, or is there some controls that should control the direction of aim, or....
This line if code is what is causing your shooting to be to the right:
move.direction = this.transform.right;
Actually it is using the right of the object this script is attached to (empty game object), not the world right. I'll give you a quick thing to play with so that you can see that you can shoot in other direction, but I'm not sure of the right fix:
move.direction = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;
Hard code or set 'angle' to some value to set the direction. A value of 0, will shoot right.
Take #2: I read your title and your comment that came through while I was commenting. I$$anonymous$$HO, the right fix is to make sure the empty game object is a child of the sprite, then change your rotation code to rotate 180 on the 'Y' rather than to use localScale. You could use the the AngleAxis() solution with your current code, but you'd also have to move your hit point when you flipped your character.
Hi, thanks for the quick reply. I have just tried to implement the changes. As you said, the Quaternion angle does change its direction back and forward, but I realised it doesn't flip the whole sprite around (a backwards rocket just doesn't work)
Also, the AngleAxis works, but also flips my camera around. Is there any way of stopping the camera from moving because it is a child of the main character so it follows it around the map. The projectile again moves to the original side
stopping the camera from rotating with your character, go to Roll a ball tutorial. It will explain that.
Answer by dqflynn · Mar 11, 2014 at 11:37 PM
if you attached it to an empty GameObject, then I think I see your problem. When you tell things to go to the right or left of an object, they go to the right or left of the object. I think your problem is that you are shooting from a different point. The wrong point. I have 3 ideas for you: 1. make the E.G.O. (empty gameobject) a child of your player. 2. put the script in the character, not the E.G.O. 3. make the script flip whenever the player flips, by making the left arrow key not just move, but rotate the shooter as well. Hope this works!
$$anonymous$$y current set up is your first idea, the empty game object is the child of the character.
The second way, does exactly the same as my original problem. The shooting still only goes to the right. I have been attempting your 3rd idea for a while, but to failure
I'm sorry that it's not working out. I'm not an experienced programmer by a long shot, I just have ideas.
(BTW, if anyone knows how to do my 3rd idea, if you put the script here it would help lots!)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Cinemachine camera rotating with player 2D 1 Answer
Fire bullet into the direction my player is facing 2D 2 Answers
2d fire from player towards mouse 1 Answer