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

Trouble with my shot orientation

Discussion in 'Scripting' started by Perci_Alex, May 18, 2020.

  1. Perci_Alex

    Perci_Alex

    Joined:
    May 18, 2020
    Posts:
    5
    Hi,
    I'm pretty new into scripting, and I have a problem with my shooting mechanic.
    I am making a 2D game in a 3D environment. The problem is pretty straight forward, when I shoot, my bullet doesn't go where I am aiming at all, and some angle just can't have bullet at all. I have made a small cylinder as a spawn point for my bullet and made the cylinder follow the mouse in the game. All of this follows my Z axis,
    • At 0° to 90° on the Z, the bullet goes straight down to progressively forward
    • between 90°to 180° the bullet goes forward
    • from -180° to -90° (180°-270°) the bullet goes backward
    • from -90° to 0 (270-0) the bullet goes backward but progressively downward as well
    Also te projectile can't go up for some reason, even if my spawn point can.

    The following script is the one I used for that matter.

    Thank you a lot for your help, and sorry for the bad English, french in my main language.
     

    Attached Files:

  2. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi! Is your SpawnOrientationScript working? If so, I think the problem comes from this line, and you may try changing it this way:
    Code (CSharp):
    1. //teleportPoint.GetComponent<Rigidbody>().AddForce(new Vector3(shotSpawn.rotation.z, 0, 0) * shotSpeed, ForceMode.Impulse);
    2.  
    3. teleportPoint.GetComponent<Rigidbody>().AddForce(shotSpawn.right * shotSpeed, ForceMode.Impulse);
    Let me know if it works :D
     
    Perci_Alex likes this.
  3. Perci_Alex

    Perci_Alex

    Joined:
    May 18, 2020
    Posts:
    5
    you're a life savior. I didn't know direction existed in unity. Thank you