- Home /
Script on Prefab is not changeable without changing all other instaniated objects
I'm sorry for crappy explanation but maybe by telling you whats happening and showing some scripts somebody would understand. I have these scripts attached to a bullet prefab which I instantiate on mouse down and have tried lots of things all of my methods are show because I turned them into comments. Heres the bullets script. Currently the bullets not moving and I think the problem may be I can't attach the public movescript move in the bullet script too an in scene object which is the player, how would I go about doing this.
public EnemyScripts enemyscript;
public MoveScript Move;
//public GameObject Enemy;
public Vector2 RightBulletVelocity = new Vector2 (30, 0);
public Vector2 LeftBulletVelocity = new Vector2 (-30, 0);
public GameObject mafia;
public float speed = 30f;
public bool BulletLeft = false;
public bool BulletRight = false;
//Vector3 CurrPos = transform.position;
//public GameObject block;
// Use this for initialization
public void BulletCheck (){
if (Move.LookingRighty = true) {
}
}
void Start () {
Destroy(gameObject, 3);
Debug.Log ("bullet has been shot");
//CurrPos.x += 0.5f;
//Rigidbody2D RB = GetComponent<Rigidbody2D> ();
//RB.velocity = new Vector2 (speed, RB.velocity.y);
//if (Move.LookingRighty == true) {
// Debug.Log("should be shooting right");
// BulletLeft = false;
// BulletRight = true;
// transform posistion +.5
//GetComponent<Rigidbody2D>().velocity = BulletVelocity;
//} else {
//GetComponent<Rigidbody2D>().velocity = BulletVelocity;
//transform.posistion -.5
// BulletLeft = true;
// BulletRight = false;
// Debug.Log("should be shooting left");
//}
//transform.position.x += 0.5f;
//transform.position = CurrPos;
//transform.position.x += 1;
}
void OnCollision2D (Collision col){
if (col.gameObject.name == "Enemy") {
enemyscript.kill ();
Destroy(gameObject);
} else {
Destroy(gameObject);}
}
// Update is called once per frame
void OnBecameInvisible(){
Destroy (gameObject);
}
void Update () {
if (BulletLeft == true) {
GetComponent<Rigidbody2D> ().velocity = LeftBulletVelocity;
Debug.Log("bullet left = true");
}
if (BulletRight == true) {
GetComponent<Rigidbody2D> ().velocity = RightBulletVelocity;
Debug.Log("bulletright = true");
}
if (transform.position.x > mafia.transform.position.x + 15) {
Destroy (gameObject);
}
if (transform.position.x < mafia.transform.position.x - 15) {
Destroy (gameObject);
}
}
}
Heres my player script
public void Shoot(){
PlayerBulletScripts.BulletCheck ();
if (LookingRighty == true) {
Rigidbody2D bulletInstance = Instantiate(bullets, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
//bulletInstance.velocity = new Vector2(bulletspeed, 0);
Debug.Log("looking right and shooting");
PlayerBulletScripts.BulletRight = true;
PlayerBulletScripts.BulletLeft = false;
//PlayerBulletScripts.BulletCheck();
}
if (LookingLefty == true) {
Rigidbody2D bulletInstance = Instantiate(bullets, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
//bulletInstance.velocity = new Vector2(-bulletspeed, 0);
Debug.Log("looking left and shooting");
PlayerBulletScripts.BulletLeft = true;
PlayerBulletScripts.BulletRight = false;
//PlayerBulletScripts.BulletCheck();
}
Debug.Log ("shoot has been called");
}
Answer by GiyomuGames · Aug 04, 2015 at 03:04 AM
Why don't you just have bullets with rigid bodies and 0 gravity? Then after you instantiate them you can add a force and they will fly in the direction you want. You won't have to manually handle their movement.
Additionally, if your bullets are very fast then maybe you can use a simple raycast to check if the player is hitting the enemies or not. If you use OnCollision2D on fast objects you may have some trouble where the bullet go through your enemies.