Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to get my player to shoot a projectile left and right?

Discussion in 'Input System' started by RedS0611, Feb 4, 2024.

  1. RedS0611

    RedS0611

    Joined:
    Aug 14, 2023
    Posts:
    2
    Hi everyone,
    I know many many many people have asked this, but none of the answers really relate to my code (I don't think so anyways) so I have to ask again...
    So, my issue is that my player can shoot a bullet (the projectile prefab) but, it only shoots to the right and I don't know how to change it to make it go left and right... yes, I am a complete noob at this.

    But here is my code:

    (This is the ProjectilePrefab script):

    Code (CSharp):
    1. public float speed = 10.0f;
    2. private GameManager gameManager;
    3.  
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         // Makes the projectile move to the right at the set speed.
    14.         // Yea, I feel like this need to be changed to not be directed to the right lol
    15.         transform.Translate(speed * Time.deltaTime * Vector2.right);
    16.     }
    17.  
    (and this is the PlayerMovement script):

    Code (CSharp):
    1.  
    2.         // Shooting bullets
    3.         if (Input.GetKeyDown(KeyCode.Space))
    4.         {   // Instantiate makes the projectile spawn at the player's position.
    5.             Instantiate(projectilePrefab, new Vector3(transform.position.x + 1, transform.position.y), projectilePrefab.transform.rotation);
    6.         }
    I really hope someone can help me, thank you guys so muchh!!!
     
    Last edited: Feb 4, 2024
  2. RedS0611

    RedS0611

    Joined:
    Aug 14, 2023
    Posts:
    2
    Never mind I fixed it! :D:D