- Home /
Randomized Gun Spread
I want a gun that changes the position a little as if it was inaccurate. heres my script. also it doesn't seem to be working
var Damage = 1;
var spread : float = 0.25;
var Cam : Camera;
private var lineTransform : Vector3;
private var startTransform : Vector3;
function Update ()
{
lineTransform = transform.position;
startTransform = transform.position;
var hit : RaycastHit;
var ray : Ray = Cam.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5,0));
Debug.DrawRay(startTransform, lineTransform, Color.red);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 500))
{
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
lineTransform = hit.point;
}
}
}
What do you mean by, "also it doesn't seem to be working?" There is an easy hack to get what you want for a random variation, but there would be no way for you to test it if you have other issues in your code. I think you need to get the code working as is before adding a variation.
Quick Hack
In the inspector change the value of spread to 0.02 to start.
Then change line 15 to these two lines:
var v = Vector3(Random.Range(0.5-spread, 0.5+spread), Random.Range(0.5-spread, 0.5+spread), 0.0);
var ray : Ray = Cam.ScreenPointToRay(v);
Note the further the target is away from the camera, the greater the variation will be in the end points of the cast ray.
For the actual random you can also use insideUnitCircle and multiply with a spread value. $$anonymous$$ight be more performant.
Not sure if insideUnitCircle would be more performat, but the code might look like:
var v = Vector3(0.5, 0.5, 0.0);
v += Random.insideUnityCircle * spread;
var ray : Ray = Cam.ScreenPointToRay(v);
thanks robertbu, i already di what you had done woth the vector3 but i didnt do Cam.ScreenPointToRay(v); and thanks everyone else for answering
Answer by supernat · Feb 01, 2014 at 06:55 AM
The variable spread doesn't appear in this script. You are setting lineTransform at start of Update, setting the line transform variable at the end of the Update method, and not using it again after that. Did you mean to set it at start of Update? Also, in the RayCast, you probably want to set it to hit.Point - lineTransform because You are passing lineTransform in as the ray direction value. Go here to see how to properly use drawRay: http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html
For creating a random range between two floats, use: float ran = Random.Range(-1.0f, 1.0f);
For instance, this produces a random range between -1 and 1.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132233im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why are my guns shooting lower when my character moves forward? 1 Answer
Weapon random movement 0 Answers
Using CrossHair GUI to Determine Ray Spread 0 Answers
How can i change a variable on the object hit by a raycast C# 2 Answers
Keep random Instantiation from spawning ontop of other objects 1 Answer