bullets not heading in the right direction, using Vector3.moveTowards
Hello all, I'm making a simplistic 2d space shooter and am having an issue when it comes to firing bullets into he correct direction, the way I have it currently is I have a child object called "firingDirection" attached to the player located far in front that will rotate to represent whatever direction the player is orientated in. After the bullet is instantiated in the start method I use Vector3.moveTowards to move the bullet to where the firingDirection game object was located when the start method runs. This works just fine when pressing play and only firing while rotating the player without moving.
The problem occurs after I have moved the player around a bit that it simply starts to fire in the wrong direction as shown in the picture below.
please help as I cannot figure this out I will post the bullet and player movement scripts below, thankyou!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullets : MonoBehaviour
{
private Rigidbody2D rb;
private GameObject firingDirection;
public float speed;
// Start is called before the first frame update
void Start()
{
firingDirection = GameObject.FindGameObjectWithTag("firingDirection");
rb = GetComponent<Rigidbody2D>();
rb.velocity = Vector3.MoveTowards(transform.position, firingDirection.transform.position, speed * Time.deltaTime);
}
// Update is called once per frame
void Update()
{
Invoke("Destroy", 3f);
}
void Destroy()
{
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject frontFacing, backEnd, standardBullet, cannon;
public float speed, rotateSpeed, thrust, maxThrust;
private Rigidbody2D rb;
private bool moveForward = false;
private bool moveCounterClockwise = false;
private bool moveClockwise = false;
private bool needsDrag = false;
private float zRotation;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
rb.drag = 0;
moveForward = true;
}
else if (!Input.GetKey(KeyCode.W))
{
//when simply drifting forward drag is applied to slow ship down faster
rb.drag = 3;
moveForward = false;
}
if (Input.GetKey(KeyCode.A))
{
rb.drag = 3;
moveCounterClockwise = true;
}
else if (!Input.GetKey(KeyCode.A))
{
moveCounterClockwise = false;
}
if (Input.GetKey(KeyCode.D))
{
rb.drag = 3;
moveClockwise = true;
}
else if (!Input.GetKey(KeyCode.D))
{
moveClockwise = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(standardBullet, cannon.transform.position, Quaternion.identity);
}
}
void FixedUpdate()
{
if (moveForward && moveCounterClockwise == false && moveClockwise == false)
{
rb.AddForce((frontFacing.transform.position - transform.position) * thrust * Time.deltaTime);
if (thrust >= maxThrust)
{
thrust = maxThrust;
}
}
if (moveCounterClockwise)
{
needsDrag = true;
zRotation += speed * 60 * Time.deltaTime;
transform.rotation = Quaternion.Euler(0, 0, zRotation);
}
if (moveClockwise)
{
needsDrag = true;
zRotation -= speed * 60 * Time.deltaTime;
transform.rotation = Quaternion.Euler(0, 0, zRotation);
}
}
}
Your answer
Follow this Question
Related Questions
Enemy MoveTowards getting stuck on obstacles 1 Answer
Help Spawn Bullet 2D 0 Answers
2D topdown projectiles movement 0 Answers
A 2D Character Controller with Rigidbody2D.AddForce 2 Answers
How to connect a set number of nodes on a map into a grid? 0 Answers