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

Question Can't set the correct rotation for a bullet

Discussion in 'Scripting' started by Sersufer, Dec 23, 2022.

  1. Sersufer

    Sersufer

    Joined:
    Apr 11, 2019
    Posts:
    3
    Hi everyone,

    I'm creating a weapon script for a FPS. To create the spread of the bullets I'm picking random points in a circle with the center at a specified distance from the world forward. Then I rotate those points to aim at where the player is looking towards. It works perfectly fine while the Z is 0, increasing the rotation causes the bullets to stop going in the camera's direction.

    This is the code for the spread of each bullet shoot.
    Code (CSharp):
    1. Vector3 efectiveRangePoint = Vector3.forward * advancedSettings.effectiveRange;
    2.  
    3.  
    4. efectiveRangePoint = efectiveRangePoint + (Vector3)UnityEngine.Random.insideUnitCircle * (advancedSettings.spreadRadius);
    5.  
    6.  
    7. Quaternion rotationInUp = Quaternion.AngleAxis(transform.rotation.eulerAngles.y, Vector3.up);
    8.  
    9. Quaternion rotationInRight = Quaternion.AngleAxis(cameraHolderTransform.localEulerAngles.x + recoilTransform.localEulerAngles.x + transform.rotation.eulerAngles.x, rotationInUp * Vector3.right);
    10.  
    11. Quaternion rotationInForward = Quaternion.AngleAxis(transform.rotation.eulerAngles.z, rotationInUp *rotationInRight * Vector3.forward);
    12.  
    13. Vector3 finalPoint = rotationInUp * efectiveRangePoint;
    14.            
    15.  
    16. finalPoint = rotationInRight * finalPoint;
    17.  
    18. finalPoint = rotationInForward * finalPoint;
    19.  
    This is the hierarchy of the Player:
    upload_2022-12-23_21-41-3.png
    • Player contains the Weapon script.
    • CameraRot contains the script to rotate the camera that rotates itself in X to look up and down, and the Parent around Y. (the cameraHolderTransform in the script above)
    • CameraRecoil contains a Recoil script that rotates in X itself to add extra rotation for each shoot. (the recoilTransform in the script above)
    Thanks in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,964
    I have no idea what all that is.

    Shoot straight ahead with

    Code (csharp):
    1. Vector3 direction = gunTransform.forward;
    Spray randomly with:

    Code (csharp):
    1. Vector3 direction = gunTransform.forward + Random.insideUnitCircle * BulletRandomness;
    EDIT: My bad... Substitute Random.onUnitCircle above...

    Where BulletRandomness might be:

    Code (csharp):
    1. float BulletRandomness = 0.02f;
     
    Last edited: Dec 24, 2022
    chemicalcrux likes this.
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    If you want to randomize the direction, I would just do this:

    Code (CSharp):
    1. Vector3 dir = [pick a normalized direction];
    2. dir += Random.onUnitSphere * 0.1f;
    3. dir = dir.normalized;
    This will add a little bit of randomness to the vector, then turn it back into a unit vector.

    ...bah, you beat me to it while I was typing this!
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,964
    Yours is better because you normalized it. :)
     
  5. Sersufer

    Sersufer

    Joined:
    Apr 11, 2019
    Posts:
    3
    Hi, thanks for your reply. But that would not work because Random.insideUnitCircle return only values in X and Y so will end up with something like this unless you are facing Vector3.forward.

    upload_2022-12-24_1-7-34.png

    This is the result of a shoot using your code, that's why the rotations are needed if using Random.insideUnitCircle, but using Random.insideUnitySphere solves all problems as chemicalcrux said.
     
  6. Sersufer

    Sersufer

    Joined:
    Apr 11, 2019
    Posts:
    3
    Fu**, I was so focused on the rotations that I didn't think that using a sphere would be almost the same in the end. The final code that works for me is this.

    Code (CSharp):
    1. Vector3 efectiveRangePoint = cam.transform.forward * advancedSettings.effectiveRange;
    2.  
    3. efectiveRangePoint = efectiveRangePoint + (Vector3)UnityEngine.Random.insideUnitCircle * (advancedSettings.spreadRadius);
    Being efectiveRangePoint the destination point where the Raycast of a single bullet would be directed torwards.

    Thanks a lot!!!