Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assigning bullet velocity in 2 directions

Discussion in '2D' started by matturp, Dec 13, 2014.

  1. matturp

    matturp

    Joined:
    Dec 13, 2014
    Posts:
    3
    Hi, I've got a 2D sprite for a platformed that's set to flip depending on input, as well as instantiating a bullet depending on which way he's facing:

    Code (CSharp):
    1.     protected Animator animator;
    2.     bool run = false;
    3.     bool jump = false;
    4.     public Vector2 jumpForce = new Vector2(0, 400);
    5.     public bool facingRight = true;            // For determining which way the player is currently facing.
    6.     public Transform bulletPrefab;
    7.     public float bulletOffset = 1;
    8.     public float fireRate = 5;
    9.     private float timeBetweenShots;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.         timeBetweenShots = 1 / fireRate;
    14.         animator = GetComponent<Animator>();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.         rigidbody2D.velocity = new Vector2 (Input.GetAxis ("Horizontal") * 5, rigidbody2D.velocity.y);
    21.        
    22.         if (Input.GetKeyUp ("space")) {
    23.             if (rigidbody2D.velocity.y == 0) {
    24.                 rigidbody2D.velocity = Vector2.zero;
    25.                 rigidbody2D.AddForce (jumpForce);
    26.             }
    27.         }
    28.         if (Input.GetKey (KeyCode.F)) {
    29.             Fire ();
    30.         }
    31.  
    32.         MoveCharacter ();
    33.     }
    34.    
    35.     double nextFire;
    36.     private void Fire (){
    37.         // Logic to check when to fire
    38.         if (Time.time > nextFire)
    39.         {
    40.             nextFire = Time.time + timeBetweenShots;
    41.             // Create bullet and fire !
    42.             if(facingRight)
    43.                 Instantiate(bulletPrefab, transform.position + transform.right * bulletOffset, transform.rotation);
    44.  
    45.             else if(!facingRight)
    46.                 Instantiate(bulletPrefab, transform.position + -transform.right * bulletOffset, transform.rotation);
    47.            
    48.         }
    49.     }
    50.    
    51.    
    52.    
    53.     void FixedUpdate ()
    54.     {
    55.         // Cache the horizontal input.
    56.         float h = Input.GetAxis("Horizontal");
    57.        
    58.         // If the input is moving the player right and the player is facing left...
    59.         if(h > 0 && !facingRight)
    60.             // ... flip the player.
    61.             Flip();
    62.         // Otherwise if the input is moving the player left and the player is facing right...
    63.         else if(h < 0 && facingRight)
    64.             // ... flip the player.
    65.             Flip();
    66.        
    67.     }
    68.    
    69.    
    70.     void Flip()
    71.     {
    72.         // Switch the way the player is labelled as facing.
    73.         facingRight = !facingRight;
    74.        
    75.         // Multiply the player's x local scale by -1.
    76.         Vector3 theScale = transform.localScale;
    77.         theScale.x *= -1;
    78.         transform.localScale = theScale;
    79.     }
    My problem is that once the bullet is instantiated I can't get it to travel left if the player is facing left (it always moves to the right). Here's the bullet code:

    Code (CSharp):
    1.     public float bulletSpeed = 20;
    2.     public float timeToDie = 3;
    3.     float deathTime;
    4.     public bool facingRight = true;
    5.     public int scoreValue;
    6.     private GameController gameController;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         if (facingRight)
    11.             rigidbody2D.velocity = transform.right * bulletSpeed;
    12.  
    13.         else if(!facingRight)
    14.             rigidbody2D.velocity = -transform.right * bulletSpeed;
    15.  
    16.         deathTime = Time.time + timeToDie;
    17.  
    Any help please? Thanks!
     
  2. dorpeleg

    dorpeleg

    Joined:
    Aug 20, 2011
    Posts:
    250
    Where are you changing the facingRight in the bullet script?
     
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    i am not sure, but i think you need use GetComponent from Player or bullets facingRight will be ever true;

    bullet script need:

    public GameObject player;
    public NameScriptWhereFacingRight namescript ;
    void Awake () {
    player = GameObject.Find ("NameOfPlayer");
    //better is findgameobjectwithtag
    namescript = player.CetComponent <NameScriptWhereFacingRight> ();}

    void start (){
    if (namescript.facingRight).........................

    its not exactly :D

    other way, make 2 prefabs for bullet. One for left, one for right, and then

    1. if(facingRight)
    2. Instantiate(bulletPrefabRight,

    3. else if(!facingRight)
    4. Instantiate(bulletPrefabLeft
     
    Last edited: Dec 18, 2014