Working on 2d orthographic view type of deal here (space invaders w full screen movement) Trying to get bullet to destroy if reaching certain location or higher (offscreen) cannont get this to work right, here's my code:
using UnityEngine;
using System.Collections;
public class shipMove : MonoBehaviour {
public GameObject ship;
Vector3 moveLeft;
Vector3 moveRight;
Vector3 moveUp;
Vector3 moveDown;
public int speed;
public Rigidbody bullet;
public int bulletSpeed;
public Vector3 bulletCorrection;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.LeftArrow)&& (ship.transform.position.x > -220) || Input.GetKey(KeyCode.A) && (ship.transform.position.x > -220))
{
transform.Translate(speed * Vector3.left * Time.deltaTime);
}
if(Input.GetKey(KeyCode.RightArrow) && (ship.transform.position.x < 220) || Input.GetKey(KeyCode.D) && (ship.transform.position.x < 220))
{
transform.Translate(speed * Vector3.right * Time.deltaTime);
}
if(Input.GetKey(KeyCode.UpArrow) && (ship.transform.position.z < 65) || Input.GetKey(KeyCode.W) && (ship.transform.position.z < 65))
{
transform.Translate(speed * Vector3.forward * Time.deltaTime);
}
if(Input.GetKey(KeyCode.DownArrow) && (ship.transform.position.z > -250) || Input.GetKey(KeyCode.X) && (ship.transform.position.z > -250))
{
transform.Translate(speed * Vector3.back * Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.Space))
{
Rigidbody clone;
clone = (Rigidbody)Instantiate(bullet, (ship.transform.position + bulletCorrection), ship.transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.forward * bulletSpeed);
if(clone.transform.position.z > 170)
{
DestroyObject(clone);
}
}
}
}
Any help would be great, thanks!
Thank you for your response. This makes sense to me yet I didn't stress that I'm very much a beginner at this...although I'm sure it's evident. A list is basically an array if I understand correctly (after searching around a bit) that I would put the rigidbodies into, check their locations, destroy when outside of parameters and remove that object from the list I just have no idea how to implement the list Any chance you have a reference link where I could review this. When using the Unity Scripting Reference I am unsure what to search for exactly.
– anon40947944Sorry about that, I saw you created a new post for the list example, and they already provided you with it. I've been away for a while and didn't check any replies so far. Hope you got it working by now, if not, reply here, and I shall provide any help I can for you.
– zharramadar