How to avoid Bullets to Change speed while flying,How to avoid Bullets speed changing while the Bullet is flying
I wanted to make a Top Down 2D Shooter and I am currently working on the Shooting. The Problem i am facing is that after Shooting a bullet and the next one should have a diferent direction. The direction of both bullets gets set to the same. So after Shooting some bullets they all have the same direction depending on where i move. So how do i make sure that only 1 Bullet gets the Speed vector at a Time?
Here is my Code:
public class Player : MonoBehaviour { public GameObject Bullet;
[SerializeField]
Sprite []Guns;
[SerializeField]
Rigidbody2D rb;
[SerializeField]
Animator Anim;
[SerializeField]
SpriteRenderer Gun;
[SerializeField]
Transform FirePos;
public Vector2 LastInputVector;
public VirtualJoystick JoystickMove;
public float speed;
Vector2 temp;
void FixedUpdate()
{
PlayerMove();
}
public void OnButtonPress()
{
Instantiate(Bullet, FirePos.position, Quaternion.identity);
}
void PlayerMove()
{
float VertMove = JoystickMove.Vertical();
float HoriMove = JoystickMove.Horizontal();
float VertSpeed = speed * VertMove;
float HoriSpeed = speed * HoriMove;
float bound = 0.4f;
if (HoriMove > bound)
{
if (VertMove > bound)
{
//UpR
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", true);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[1];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(.3f, 0.15f, 0);
this.transform.localScale = new Vector3(5, 5, 0);
}
else if (VertMove < -bound)
{
//DownR
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", true);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[0];
Gun.sortingOrder = 2;
Gun.transform.position = transform.position + new Vector3(.2f, -0.3f, 0);
this.transform.localScale = new Vector3(5, 5, 0);
}
else
{
//Right
temp = new Vector2(HoriSpeed, 0);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", true);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[3];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(.4f, -0.15f, 0);
this.transform.localScale = new Vector3(5, 5, 0);
}
}
else if (HoriMove < -bound)
{
if (VertMove > bound)
{
//UpL
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", true);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[1];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(-.3f, 0.15f, 0);
this.transform.localScale = new Vector3(-5, 5, 0);
}
else if (VertMove < -bound)
{
//DownL
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", true);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[0];
Gun.sortingOrder = 2;
Gun.transform.position = transform.position + new Vector3(-.2f, -0.3f, 0);
this.transform.localScale = new Vector3(-5, 5, 0);
}
else
{
//Left
temp = new Vector2(HoriSpeed, 0);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", true);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[3];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(-.4f, -0.15f, 0);
this.transform.localScale = new Vector3(-5, 5, 0);
}
}
else
{
if (VertMove > 0)
{
//Up
temp = new Vector2(0, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", true);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[4];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(0, .5f, 0);
}
else if (VertMove < 0)
{
//Down
temp = new Vector2(0, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", true);
Gun.sprite = Guns[2];
Gun.sortingOrder = 2;
Gun.transform.position = transform.position + new Vector3(0, -.4f, 0);
}
else
{
temp = Vector2.zero;
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
}
}
rb.velocity = temp;
LastInputVector = temp;
}
public Vector2 GetInputVector()
{
return LastInputVector;
}
}
public class BulletScript : MonoBehaviour {
public Rigidbody2D BulletRb;
public Player player;
public void Awake()
{
BulletRb = GetComponent<Rigidbody2D>();
player = FindObjectOfType<Player>();
}
public void Update()
{
ShootBullet();
}
public void ShootBullet()
{
Vector2 dir = player.GetInputVector();
if (dir.x > 0)
{
if (dir.y > 0)
{
BulletRb.velocity = new Vector2(1, 1);
//transform.Translate(new Vector2(1, 1) * 17 * Time.deltaTime);
}
else if (dir.y < 0)
{
BulletRb.velocity = new Vector2(1, -1);
// transform.Translate(new Vector2(1, -1) * 17 * Time.deltaTime);
}
else
{
BulletRb.velocity = new Vector2(1, 0);
// transform.Translate(new Vector2(1, 0) * 17 * Time.deltaTime);
}
}
else if (dir.x < 0)
{
if (dir.y > 0)
{
BulletRb.velocity = new Vector2(-1, 1);
// transform.Translate(new Vector2(-1, 1) * 17 * Time.deltaTime);
}
else if (dir.y < 0)
{
BulletRb.velocity = new Vector2(-1, -1);
// transform.Translate(new Vector2(-1, -1) * 17 * Time.deltaTime);
}
else
{
BulletRb.velocity = new Vector2(-1, 0);
// transform.Translate(new Vector2(-1,0) * 17 * Time.deltaTime);
}
}
else
{
if (dir.y > 0)
{
BulletRb.velocity = new Vector2(0, 1);
// transform.Translate(new Vector2(0, 1) * 17 * Time.deltaTime);
}
else if (dir.y < 0)
{
BulletRb.velocity = new Vector2(0, -1);
// transform.Translate(new Vector2(0, -1) * 17 * Time.deltaTime);
}
}
Debug.Log(dir);
}
}
,I wanted to make this Top Down 2D shooter and am currently working on the Shooting. The Problem i am facing is that the Bullets when they are being shot are getting their velocity vector changed all at once. I tried really hard to make it, so that only 1 Bullet at a Time gets their Speed, but it didnt work. I am still learning unity though.
Here is my Code:
public class Player : MonoBehaviour { public GameObject Bullet;
[SerializeField]
Sprite []Guns;
[SerializeField]
Rigidbody2D rb;
[SerializeField]
Animator Anim;
[SerializeField]
SpriteRenderer Gun;
[SerializeField]
Transform FirePos;
public Vector2 LastInputVector;
public VirtualJoystick JoystickMove;
public float speed;
Vector2 temp;
void FixedUpdate()
{
PlayerMove();
}
public void OnButtonPress()
{
Instantiate(Bullet, FirePos.position, Quaternion.identity);
}
void PlayerMove()
{
float VertMove = JoystickMove.Vertical();
float HoriMove = JoystickMove.Horizontal();
float VertSpeed = speed * VertMove;
float HoriSpeed = speed * HoriMove;
float bound = 0.4f;
if (HoriMove > bound)
{
if (VertMove > bound)
{
//UpR
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", true);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[1];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(.3f, 0.15f, 0);
this.transform.localScale = new Vector3(5, 5, 0);
}
else if (VertMove < -bound)
{
//DownR
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", true);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[0];
Gun.sortingOrder = 2;
Gun.transform.position = transform.position + new Vector3(.2f, -0.3f, 0);
this.transform.localScale = new Vector3(5, 5, 0);
}
else
{
//Right
temp = new Vector2(HoriSpeed, 0);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", true);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[3];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(.4f, -0.15f, 0);
this.transform.localScale = new Vector3(5, 5, 0);
}
}
else if (HoriMove < -bound)
{
if (VertMove > bound)
{
//UpL
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", true);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[1];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(-.3f, 0.15f, 0);
this.transform.localScale = new Vector3(-5, 5, 0);
}
else if (VertMove < -bound)
{
//DownL
temp = new Vector2(HoriSpeed, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", true);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[0];
Gun.sortingOrder = 2;
Gun.transform.position = transform.position + new Vector3(-.2f, -0.3f, 0);
this.transform.localScale = new Vector3(-5, 5, 0);
}
else
{
//Left
temp = new Vector2(HoriSpeed, 0);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", true);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[3];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(-.4f, -0.15f, 0);
this.transform.localScale = new Vector3(-5, 5, 0);
}
}
else
{
if (VertMove > 0)
{
//Up
temp = new Vector2(0, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", true);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
Gun.sprite = Guns[4];
Gun.sortingOrder = 0;
Gun.transform.position = transform.position + new Vector3(0, .5f, 0);
}
else if (VertMove < 0)
{
//Down
temp = new Vector2(0, VertSpeed);
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", true);
Gun.sprite = Guns[2];
Gun.sortingOrder = 2;
Gun.transform.position = transform.position + new Vector3(0, -.4f, 0);
}
else
{
temp = Vector2.zero;
Anim.SetBool("IsDiagDownR", false);
Anim.SetBool("IsDiagDownL", false);
Anim.SetBool("IsDiagUpR", false);
Anim.SetBool("IsDiagUpL", false);
Anim.SetBool("IsUp", false);
Anim.SetBool("IsSide", false);
Anim.SetBool("IsDown", false);
}
}
rb.velocity = temp;
LastInputVector = temp;
}
public Vector2 GetInputVector()
{
return LastInputVector;
}
public class BulletScript : MonoBehaviour {
public Rigidbody2D BulletRb;
public Player player;
public void Awake()
{
BulletRb = GetComponent<Rigidbody2D>();
player = FindObjectOfType<Player>();
}
public void Update()
{
ShootBullet();
}
public void ShootBullet()
{
Vector2 dir = player.GetInputVector();
if (dir.x > 0)
{
if (dir.y > 0)
{
BulletRb.velocity = new Vector2(1, 1);
//transform.Translate(new Vector2(1, 1) * 17 * Time.deltaTime);
}
else if (dir.y < 0)
{
BulletRb.velocity = new Vector2(1, -1);
// transform.Translate(new Vector2(1, -1) * 17 * Time.deltaTime);
}
else
{
BulletRb.velocity = new Vector2(1, 0);
// transform.Translate(new Vector2(1, 0) * 17 * Time.deltaTime);
}
}
else if (dir.x < 0)
{
if (dir.y > 0)
{
BulletRb.velocity = new Vector2(-1, 1);
// transform.Translate(new Vector2(-1, 1) * 17 * Time.deltaTime);
}
else if (dir.y < 0)
{
BulletRb.velocity = new Vector2(-1, -1);
// transform.Translate(new Vector2(-1, -1) * 17 * Time.deltaTime);
}
else
{
BulletRb.velocity = new Vector2(-1, 0);
// transform.Translate(new Vector2(-1,0) * 17 * Time.deltaTime);
}
}
else
{
if (dir.y > 0)
{
BulletRb.velocity = new Vector2(0, 1);
// transform.Translate(new Vector2(0, 1) * 17 * Time.deltaTime);
}
else if (dir.y < 0)
{
BulletRb.velocity = new Vector2(0, -1);
// transform.Translate(new Vector2(0, -1) * 17 * Time.deltaTime);
}
}
Debug.Log(dir);
}
}
Thank you for your help!
Your answer
Follow this Question
Related Questions
How to give the player a limited amount of moves? 1 Answer
How to prevent objects to push in 2d top-down? 0 Answers
Shoot at mouse cursor 0 Answers
[2D] Make player push object 1 Answer