[Simply Question] Making Unity's Raycast not so "Straight"

Basically for a gun-game I am designing I use bullets as a Raycast. But I don’t want those bullets to be 100% straight, so I am curious how I would go about “Curving” them.

Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);

I tried simply adding to the camera.main.transform.forward, but realized that was absurd as it’s a vector3 and can’t just get “Added” to. I then considered adding to only one axis but then that would be in global space not self so that’s no good. Anyone have any ideas? Any help is greatly appreciated!

Sincerely,
Blake Gillman

Multiple raycasts with a non-infinite maxDistance.

Basically do a raycast x units forward. Make new ray starting at that end position, move ray direction down some amount, raycast x units forward. Repeat.

I had considered something similar but I’m not exactly fluent with how their raycasts work, could you give an example?

Like just give it a max distance of like 20 then shoot another ray aiming slightly down? If so, how would I make it aim down, the purpose of this question was sort of “How to make it not go straight” rather than just doing transform.forward - Because I don’t know how to curve it slightly. All I know is transform.forward so if I kept shooting off rays then it would still just be a straight line, I don’t know how to curve it down or wherever even slightly! I just know the transform.forward, right, etc.

Couldn’t you move the ray a few degrees randomly? I would generate a random int, then add it to one of the axis rotations (X or Y) so that it would point slightly differently. Or you could also have real bullet effects by instantiating a bullet that would do the randomization with its own script.

Exactly what you described is what I want to do, but how do I feed that to the ray is my question - I tried doing something like

Vector3 newVec = new Vector3(0,0,1);

Ray is (camera.forward + new vec)

Obviously that not being real code, just an example. But it didn’t work. I’m not sure how I should be feeding the ray to point slightly different? Also I don’t use bullet instantiations because I heard they move fast to detect collisions properly.

EDIT:
I also want to make sure that it feeds it local axis, because adding to its Z if its in global is no good

Took it on myself as a challenge, as I’ll need this in the future too:

using UnityEngine;
using System.Collections;
using System.Linq;

public class TESTScript : MonoBehaviour {

    //how much we move
    public float RandomAmount;
    //min-max range of how much we want to move
    public float RandomRange = 0.01f;
    // self-explanatory name
    public Vector3 NewRotation;
    // Use this for initialization
    void Start ()
    {
        RandomAmount = Random.Range(RandomRange * -1, RandomRange);
    }
   
    // Update is called once per frame
    void FixedUpdate ()
    {
        //create random amount
        RandomAmount = Random.Range(RandomRange * -1, RandomRange);
        //randomize Z and Y
        float NewZ = RandomAmount += transform.eulerAngles.z;
        RandomAmount = Random.Range(RandomRange * -1, RandomRange);
        float NewY = RandomAmount += transform.eulerAngles.y;
        //rotate
        NewRotation = new Vector3(transform.rotation.x, NewY, NewZ);
        //shoot it
        ShootRay();
        //see in editor
        Debug.DrawRay(transform.position, NewRotation * 100, Color.red);
    }

    void ShootRay()
    {
        Ray ray = new Ray(this.transform.position, NewRotation);
    }
}

Should simulate an assault-rifle-style dispersion.

Because I have to use locally assigned variables and the firing happens right there, I had to re-design your script somewhat. I managed to come up with my own but it doesn’t seem to be working, any ideas why?

(weaponData.throwOff is assigned to be 0.01).

float RandomAmount;
        Vector3 NewRotation;

        RandomAmount = Random.Range (-weaponData.throwOff, weaponData.throwOff);
        float NewZ = RandomAmount += transform.eulerAngles.z;
        float NewY = RandomAmount += transform.eulerAngles.y;

        NewRotation = new Vector3 (transform.rotation.x, NewY, NewZ);

        Ray ray = new Ray (Camera.main.transform.position, NewRotation);

EDIT:
Also when I aim down / up too much the bullet goes straight up o.o

EDIT 2:
Tested again - It didn’t happen this time, then I played again and it did happen - Weird stuff going on :stuck_out_tongue:

I DIDI T!
I managed to make something far simpler (And since I came up with it, I’m extra proud :smile:)!

Hope this helps you as well, because it sure solves my issue!

Vector3 NewRotation = Camera.main.transform.forward;

        NewRotation.z += Random.Range (-weaponData.throwOff, weaponData.throwOff);
        NewRotation.y += Random.Range (-weaponData.throwOff, weaponData.throwOff);

        Ray ray = new Ray (Camera.main.transform.position, NewRotation);

You of course can replace weaponData.throwOff with the RandomAmount variable you were doing!

Glad to see it worked for you, the exact execution isn’t as important as the result. I’m glad you could find parts of my script that helped you make the ultimate solution for your project :slight_smile:

1 Like

Well thank you, and glad you could help :slight_smile:

EDIT:
You should also be told that the RandomRange * -1 is not a good way to go just do -RandomRange.

I mean like:

What you have
RandomRange * -1, RandomRange

You could just do:
-RandomRange, RandomRange

It makes things a little easier.
Sincerely,
Blake Gillman

1 Like

Yup, that shows just how much of a noob I am at scripting :confused: