C# 2D Shooting a bullet based on player direction
I'm fairly new to unity so this probably isn't the best way to do this but the bullet goes in the right direction but if the player changes direction, the bullet changes direction, I knew this would happen but I have no solution to fix it. Thanks!
Player Code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public int direction = 0;
public float speed = 0;
public int mana = 0;
public Bullet myBullet;
public KeyCode shoot;
public KeyCode moveUp;
public KeyCode moveDown;
public KeyCode moveLeft;
public KeyCode moveRight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Movement();
Shoot();
}
void Shoot () {
if (Input.GetKey(shoot) && mana > 0)
{
Instantiate(myBullet);
mana -= 1;
}
}
void Movement () {
if (Input.GetKey(moveUp))
{
transform.Translate(Vector2.up * speed);
direction = 1;
}
if (Input.GetKey(moveDown))
{
transform.Translate(Vector2.down * speed);
direction = 2;
}
if (Input.GetKey(moveLeft))
{
transform.Translate(Vector2.left * speed);
direction = 3;
}
if (Input.GetKey(moveRight))
{
transform.Translate(Vector2.right * speed);
direction = 4;
}
}
}
Bullet Code:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
public float speed = 1;
// Use this for initialization
void Start()
{
GameObject player = GameObject.Find("Player");
Transform playerTransform = player.transform;
// get player position
Vector2 position = playerTransform.position;
transform.position = new Vector2(position.x, position.y);
}
// Update is called once per frame
void Update()
{
GameObject myPlayer = GameObject.Find("Player");
Player player = myPlayer.GetComponent<Player>();
if (player.direction == 1)
{
ShootUp();
}
else if (player.direction == 2)
{
ShootDown();
}
else if (player.direction == 3)
{
ShootLeft();
}
else if (player.direction == 4)
{
ShootRight();
}
}
void ShootUp()
{
transform.Translate(Vector2.up * speed);
}
void ShootDown()
{
transform.Translate(Vector2.down * speed);
}
void ShootLeft()
{
transform.Translate(Vector2.left * speed);
}
void ShootRight()
{
transform.Translate(Vector2.right * speed);
}
}
Answer by DrGEEK · Apr 10, 2016 at 11:58 AM
You just need to store the direction in a variable when you create the bullet, then, move the bullet with the vector in the variable
Bullet code:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
public float speed = 1;
private Vector2;
// Use this for initialization
void Start()
{
GameObject player = GameObject.Find("Player");
Transform playerTransform = player.transform;
// get player position
Vector2 position = playerTransform.position;
transform.position = new Vector2(position.x, position.y);
GameObject myPlayer = GameObject.Find("Player");
Player player = myPlayer.GetComponent<Player>();
}
// Update is called once per frame
void Update()
{
//i moved the player vector update to the start function
if (player.direction == 1)
{
ShootUp();
}
else if (player.direction == 2)
{
ShootDown();
}
else if (player.direction == 3)
{
ShootLeft();
}
else if (player.direction == 4)
{
ShootRight();
}
}
void ShootUp()
{
transform.Translate(Vector2.up * speed);
}
void ShootDown()
{
transform.Translate(Vector2.down * speed);
}
void ShootLeft()
{
transform.Translate(Vector2.left * speed);
}
void ShootRight()
{
transform.Translate(Vector2.right * speed);
}
}
I think the script would be like that
I already tried that and it only moved a little bit, also you can't access the variables in the start function in the update function if they're not public.
Your answer
Follow this Question
Related Questions
Trying to get Player to shoot in both directions depending on what side he is facing? 0 Answers
Make bullet face direction of movement 1 Answer
Unity 2d simple c# shooting script 1 Answer
No overload for method 'fireBullet' takes 0 arguments 1 Answer
Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers