Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jumping and shooting knocks character off balance

Discussion in '2D' started by TheLiverMan, Aug 29, 2017.

  1. TheLiverMan

    TheLiverMan

    Joined:
    Aug 17, 2017
    Posts:
    15
    Whenever I start shooting, everything works out fine (the bullets come out, they go in a certain direction.) But for some reason when my character is in the air and fires, the bullet seems to hit him and he starts spinning.

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class BulletCtrl : MonoBehaviour {
    8.  
    9.     public Vector2 bulletspeed;
    10.  
    11.     [SerializeField]
    12.     private float delay;
    13.     Collider2D myCollider;
    14.     Rigidbody2D rb2d;
    15.     Collider2D coll;
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.         coll = GameObject.FindGameObjectWithTag ("Player").GetComponent<Collider2D> ();
    20.         myCollider = GetComponent<Collider2D> ();
    21.        
    22.         rb2d = GetComponent<Rigidbody2D> ();
    23.         rb2d.velocity = bulletspeed;
    24.         Destroy (gameObject, delay);
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.         rb2d.velocity = bulletspeed;
    31.         Physics2D.IgnoreCollision(coll, myCollider, true);
    32.  
    33.  
    34.     }
    35.  
    36.  
    37. }
    38.  
    39.  
    Thanks for taking a look at my problem.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Do the IgnoreCollision in Start or Awake. You only have to call it once.
     
  3. TheLiverMan

    TheLiverMan

    Joined:
    Aug 17, 2017
    Posts:
    15
    After doing that, the bullet still seems to collide with the character.

    EDIT: I got it working somehow, but I'm not really sure how.
     
    Last edited: Aug 30, 2017