- Home /
 
 
               Question by 
               jhartman1 · Dec 15, 2017 at 04:13 AM · 
                c#2d-platformercollision2d  
              
 
              problem Getting AI projectile to collide with player
I'm making a 2D game for a project and I seem to be having a issue getting my AI projectile to collide with the player. I looked at the components, my collider on the projectile is trigger, its on the same layer, my tags are set up correctly . but the object just passes right by the player. below is my player script where it detects collisions or in this case for a OnTriggerEnter() and will repawn the player at a checkpoint. // refrences the level manager script public LevelManager levelManager;
 //how fast the spider moves
 [SerializeField]
 public float MoveSpeed = 4f;  
 private Transform myTransform;    // reference to the object's transform
 float previousPositionX;
 public float jumpPower;
 bool grounded = false;
 public Projectile Bullet;
 //rate of fire
 [SerializeField]
 public float FireRate = 0.5f;
 //time since last shot
 private float FireTimer = 0.0f;
 [SerializeField]
 WeaponsContoller myWeapon;
 [SerializeField]
 //int MaxLives = 1;
 //The current lives remaining for the player
 //private int CurLives =1; 
 public bool isleft = true;
 private SpriteRenderer mySpriteRenderer;
 // Use this for initialization
 void Start () {
     myTransform = transform;
     previousPositionX = myTransform.position.x;
     mySpriteRenderer = GetComponent<SpriteRenderer> ();
     myWeapon = GetComponentInChildren<WeaponsContoller> ();
 }
 // FixedUpdate is called once per physics tick/frame
 void Update () {
     Rigidbody2D rb2d = gameObject.GetComponent<Rigidbody2D>();
     if (Input.GetKey (KeyCode.RightArrow)) 
     {
         mySpriteRenderer.flipX= false;
         isleft = false;
     } 
     if (Input.GetKey (KeyCode.LeftArrow)) 
     {
         mySpriteRenderer.flipX = true;
         isleft = true;
     } 
     float translate = Input.GetAxis ("Horizontal") * MoveSpeed;
     Vector3 horizontal = new Vector3 (translate,0,0);
     transform.Translate (horizontal * Time.deltaTime);
     if (Input.GetKey (KeyCode.Space)) {
         if (grounded) {
             GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpPower);
             //Bottom code causes delay jump, sorta like glide, could be useful for gliding feature
             //GetComponent<Rigidbody2D> ().position= new Vector2 (GetComponent<Rigidbody2D> ().position.x, MoveSpeed );
         }
     }
     if (Input.GetKeyDown (KeyCode.B)) {
         MoveSpeed *= 2.0f;
     } 
     else if (Input.GetKeyUp (KeyCode.B)) {
         MoveSpeed /= 2.0f;
     } 
     
 }
 void UpdateFiring()
 {
     // Accumualte time each frame. When the fire key is pressed, we check if enough time has passed.
     FireTimer += Time.deltaTime;
     //Detect if the fire button has been pressed.
     if (Input.GetKeyDown(KeyCode.F))
         Debug.Log("button was hit");
     {
         // Has the Fire timer exceeded the amount of time between the spawning of projectiles?
         if (FireTimer > FireRate)
         {
             //Reset the timre so it will start counting from scratch for the next shot.
             FireTimer = 0;
             //Call the function which handles the spawning of projectiles.
             DoWeaponFire();
             print("The fire button has been pressed!");
         }
     }
 }
 //check if grounded
 void OnTriggerEnter2D(Collider2D col)
 {
     grounded = true;    
     if (col.gameObject.tag == "Enemy") {
         levelManager.RespawnPlayer ();
         if (col.gameObject.tag == "Enemy Projectile") {
             levelManager.RespawnPlayer ();
         }
     }
 }
 void OnTriggerExit2D()
 {
     grounded = false;
 }
 void DoWeaponFire(){
     print ("the \"DoWeaponFire\" function has been called");
     Instantiate (Bullet, transform.position, transform.rotation);
 } 
      
 void LateUpdate()
 {
     previousPositionX = myTransform.position.x;
 }
 
              
               Comment
              
 
               
              Your answer