Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Domo23000 · Feb 01, 2014 at 06:28 AM · raycastrandomgunfloatspread

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;
         } 
     }
 }

alt text

awerds.png (164.2 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 01, 2014 at 06:42 AM 0
Share

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.

avatar image zaurek · Feb 01, 2014 at 06:50 AM 0
Share

For the actual random you can also use insideUnitCircle and multiply with a spread value. $$anonymous$$ight be more performant.

avatar image robertbu · Feb 01, 2014 at 07:04 AM 0
Share

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); 
avatar image Domo23000 · Feb 01, 2014 at 04:46 PM 0
Share

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

avatar image Domo23000 · Feb 01, 2014 at 04:46 PM 0
Share

ill also try yours zaurek

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges