- Home /
Regarding projectile scripting
I am making a platformer for my Cousera tutorials where I have made a trap that shoots projectiles on a horizontal 'x' direction. The problem is, I can't figure out how to change the direction. All the projectiles are shooting at positive 'x' direction, but one of them is supposed to shoot at negative 'x' direction.
Here are the two scripts I have made for the projectile
public class Projectile : MonoBehaviour {
public float speed;
public GameObject explosion;
public int damage;
Rigidbody2D _rigidbody;
bool direction;
//Vector2 target;
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
if (!_rigidbody)
Debug.LogError("Please add a RigidBody2D Component");
direction = gameObject.GetComponent<GunEnemy>().direction;
}
private void Update()
{
if(!direction)
transform.Translate(- speed * Time.deltaTime, 0, 0);
else
transform.Translate(speed * Time.deltaTime, 0, 0);
}
void DestroyProjectile()
{
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
CharacterController2D player = collision.gameObject.GetComponent<CharacterController2D>();
player.ApplyDamage(damage);
}
DestroyProjectile();
}
private void OnDestroy()
{
Instantiate(explosion,transform.position, transform.rotation);
Debug.Log("Explosion!!!");
}
}
Here is the main 'GunEnemy' script which calls the projectiles.
public class GunEnemy : MonoBehaviour {
public float startTimeBetweenShots;
public bool direction;
public GameObject Projectile;
float _timebetweenShots;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (_timebetweenShots <= 0)
{
Instantiate(Projectile, transform.position, Quaternion.identity);
_timebetweenShots = startTimeBetweenShots;
Debug.Log("Projectile Created!!");
}
else
{
_timebetweenShots -= Time.deltaTime;
}
}
}
Answer by dCalle · May 15, 2018 at 03:03 PM
Well you could go to GunEnemy and set direction to false.
but using a bool for direction actually is confusing. Why don't you call it shootsRight ?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
OnCollisionExit() too slow 1 Answer
Target Wheel Rotation/Physics 0 Answers
Need help with wall cover system(splinter cell like) 0 Answers