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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

shoot the monsters,but i just only shoot at right.

Discussion in '2D' started by Takiizaki, Feb 16, 2016.

  1. Takiizaki

    Takiizaki

    Joined:
    Feb 5, 2016
    Posts:
    2
    when i press down the button,it will be start to shoot projectiles,but just only shoot projectiles at right.the character can't to shoot at left.What should i do? Anyone can help me ?or getting some idea!Thanks!!
    bulletmove:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shooting : MonoBehaviour {
    5.  
    6.     public GameObject Rock;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Input.GetKeyDown (KeyCode.Z)) {
    16.             //Rigidbody clone;
    17.             Instantiate (Rock, transform.position,new Quaternion(0,0,0,0));
    18.             //clone.velocity = transform.TransformDirection (Vector3.forward * 10);
    19.         }
    20.         if (Input.GetKeyDown (KeyCode.X)) {
    21.             Instantiate (Rock, transform.position,new Quaternion (0,0,0,0));
    22.         }
    23.    
    24.     }
    25. }
    shooting:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Rockmove : MonoBehaviour {
    5.     public float BulletSpeed = 1;
    6.     //public float Bulletleft = -8;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         transform.Translate (Vector2.one * BulletSpeed * Time.deltaTime);
    16.         if (transform.position.y > 20 || transform.position.y < -20) {
    17.             Destroy (gameObject);
    18.         }
    19.     }
    20. }
    21.  
     
  2. geek_freek

    geek_freek

    Joined:
    Aug 26, 2014
    Posts:
    51
    Code (CSharp):
    1.  
    2. transform.Translate(Vector2.one*BulletSpeed*Time.deltaTime);
    3.  
    This is whats causing the problem.
    Change BulletSpeed as the player changes direction.
    Code (CSharp):
    1.  
    2. BulletSpeed=PlayerDirection;
    3.  
    so ,if PlayerDirection is 1 then bulletspeed is 1 ,
    if PlayerDirection is -1 then bulletSpeed is -1.
     
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    or you can add second bullet for left as prefab. Make it speed -1.
    public GameObject Rock, RockLeft;
    then Instantiate for shooting left, RockLeft.
     
    Takiizaki likes this.
  4. Takiizaki

    Takiizaki

    Joined:
    Feb 5, 2016
    Posts:
    2
    Oh~i got it.
    Thanks for your helps^_^