- Home /
How do I make a 2d bullet object move towards the players original position when it is spawned?
When the player enters a circle collier trigger, the turret object, which is a child of the enemy. Will rotate towards the player and shoot a bullet every 2 seconds. Right now, the bullets just spawn. I would like some help getting the bullets to move towards the player. NOTE: I don't want the bullet to follow the player, just to fly towards the position the player was at when it had spawned. Here is my code:
Enemy Shooting Script: public class EnemyShooting : MonoBehaviour {
public GameObject enemyBullet;
public Transform player;
public float shootTimer;
public GameObject enemyTurret;
public bool playerInRange;
public float rotateSpeed;
public GameObject enemyBarrel;
// Use this for initialization
void Start () {
playerInRange = false;
shootTimer = 2;
}
// Update is called once per frame
void Update () {
if(playerInRange == true)
{
Vector3 vectorToTarget = player.position - transform.position;
float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotateSpeed);
shootTimer -= Time.deltaTime;
if(shootTimer <= 0)
{
EnemyShoot();
shootTimer = 2;
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
playerInRange = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
playerInRange = false;
}
}
private void EnemyShoot()
{
GameObject bulletShoot = (GameObject)Instantiate(enemyBullet, transform.position, Quaternion.identity);
}
}
Enemy Bullet Script: public class EnemyBullet : MonoBehaviour {
public int damage;
public float speed;
public Transform player;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = transform.up;
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject GO;
if (collision.gameObject.CompareTag("Player"))
{
GO = collision.gameObject;
GO.GetComponent<PlayerMovement>().playerHealthCount -= damage;
Destroy(gameObject);
}
if (collision.gameObject.CompareTag("NorthWall"))
{
Destroy(gameObject);
}
if (collision.gameObject.CompareTag("SouthWall"))
{
Destroy(gameObject);
}
if (collision.gameObject.CompareTag("EastWall"))
{
Destroy(gameObject);
}
if (collision.gameObject.CompareTag("WestWall"))
{
Destroy(gameObject);
}
}
}
Thank you for your time.
Answer by MT369MT · May 24, 2018 at 07:15 PM
Hi, in EnemyShoot() and after instantiating the Bullet add this:
bulletShoot.GetComponent<Rigidbody2D>().velocity = (player.transform.position - transform.position).normalized * bulletSpeed;
Be sure to add a rigidbody2d to your bullet and to set its gravity scale to 0. Create also a new float called for example bulletSpeed.
To build on top of your answer, i recon it would be aesthetically pleasing to make the bullet face the same direction as the velocity, that's if the bullet's image/sprite looks like it could benefit from that (i.e not a ball).
Thank you so much! also thanks for the heads up @Tobychappell It's a ball now, but it will be changed eventually.
Answer by JusSumGuy · May 24, 2018 at 10:09 PM
This is the code for the bullet to just go to the players position at the time the bullet is instantiated.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ship2Rocket : MonoBehaviour {
[SerializeField] GameObject explosionPrefab;
[SerializeField] float speed;
float finalSpeed;
GameObject player;
Vector3 currentPlayerPos;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
if(player)
currentPlayerPos = player.transform.position;
finalSpeed = speed * Time.deltaTime;
}
void Update()
{
transform.LookAt(currentPlayerPos);
transform.position = Vector3.MoveTowards(transform.position, currentPlayerPos, finalSpeed);
if (transform.position == currentPlayerPos)
{
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Enemy Damage problem 1 Answer
Invoke is not working properly. 0 Answers
Very basic Instantiate Question 2 Answers
Instantiate after setting values? 0 Answers
Shooting a bullet object toward the mouse position in a 2D. 1 Answer