Projectile disappears instantly after appearing.
So I'm trying to add a projectile system to my game. I've seen a tutorial about it and I don't know why the projectiles I spawn only appear where my player is but then instantly disappears.Additionally, it does not move even if I just place it in the scene. here are the relevant scripts. What did I do wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectiles : MonoBehaviour
{
public float speed;
private Vector2 screenBounds;
private Rigidbody2D rb;
void Start()
{
Debug.Log("MOVING");
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(speed, 0);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height,
Camera.main.transform.position.z));
}
// Update is called once per frame
void Update()
{
//transform.position += transform.right * speed * Time.deltaTime;
if(this.transform.position.x > screenBounds.x *-2)
{
Destroy(this.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag != "Player")
{
Destroy(this.gameObject);
Destroy(collision.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public GameObject projectile;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Shoot();
}
// The input for at the horizontal axis - Keys A and D.
float horizontalIInput = Input.GetAxis("Horizontal"); // gets a value of 1 or -1
transform.Translate(new Vector3(horizontalIInput, 0, 0)*speed*Time.deltaTime);
// The input for at the vertical axis - Keys W and S.
float verticalInput = Input.GetAxis("Vertical"); // gets a value of 1 or -1 (up or down)
transform.Translate(new Vector3(0, verticalInput, 0) * speed * Time.deltaTime);
}
void Shoot()
{
GameObject p = Instantiate(projectile,this.transform.position,Quaternion.identity);
//spawns the projectile
}
}
Answer by streeetwalker · Mar 25, 2020 at 08:44 PM
this looks suspicious.
if(this.transform.position.x > screenBounds.x *-2)
You want to check if the bullet goes off the right side of the screen or the left? I'm pretty sure this is checking if your projectile position is > the left side of the screen *2. Because you're multiplying by -2. So, you are destroying it immediately.
I think you have worse problems: have you checked what your screenBounds variable's value is? You're better off converting an objects' world position to screen coordinates and checking against screen.width and screen.height for right and top bounds, and 0 and 0 for left and bottom bounds.
Thanks! that was really the problem. I copied this line from the video and it was suspicious to me too. I don't know how did it work for him for the same purpose(destroy the projectile if it passes the right side of the screen).
Your answer
Follow this Question
Related Questions
Changing rotation of prefab 0 Answers
Problem with projectile direction 0 Answers
Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers
Is there any good sample code for shooting a projectile and collision detection? 0 Answers
Instantiating prefabs, and making them move. (Projectiles) 0 Answers