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 Quenter · Jan 13, 2012 at 11:32 PM · raycastfpscrosshairs

Raycast Shooting Randomizing

I am trying to make a FPS game where as you fire, the accuracy decreases- you know, where you hold down fire on a Machine Gun and it gets less and less accurate the longer you hold it? Here is part of my fire method (I'm using Raycasts)


 var direction = transform.TransformDirection(Vector3.forward);
 
 var hit : RaycastHit;
     
 
     // Did we hit anything?
 
     if (Physics.Raycast (camera.main.gameObject.transform.position, direction, hit,  
         range)) 
     {
 
         // Apply a force to the rigidbody we hit
 
         if (hit.rigidbody)
 
             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
  



Would I have a floating point variable that would increase with every shot, and is added or subtracted from the origin of the Raycast (and it would decrease marginally every half second or so)?

Also, I want to add dynamic crosshairs (the outer bit gets bigger as you fire, representing your hit zone, while the center stays small and...well, centered); I guess that would be done with two different crosshair textures. What would the script (JavaScript) for controlling the crosshair texture that changes size look like?

Example of dynamic crosshairs (sort of)

Thank you for your time!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Jan 14, 2012 at 12:09 AM

You could use Random.InsideUnitCircle multiplied by a float variable to add an uncertainty value to the ray:

var uncert: float = 0; var uncertUp: float = 0.2; // uncertainty increased by this amount each shot var uncertDn: float = 0.5; // uncertainty reduced by this amount each second

function Update(){ if (uncert > 0) uncert -= uncertDn Time.deltaTime; // decrease uncertainty over time ... } In the shot routine, add Random.InsideUnitCircle uncert to the forward vector - since Random.InsideUnitCircle is a Vector2, only x and y will be affected:

... private var uncertFwd = Vector3.forward + uncert * Random.InsideUnitCircle; var direction = transform.TransformDirection(uncertFwd); uncert += uncertUp; // increase uncertainty each shot

var hit : RaycastHit; ...

About the crosshair: you could use four GUI.DrawTexture(rect, image) to draw them:

var size: float = 0; // sets the crosshair size var hMark: Texture2D; // drag horizontal mark image here var vMark: Texture2D; // drag vertical mark image here var v = Screen.height/2; var h = Screen.width/2; var vRect = Rect(-2, 0, 4, 9); // set this to the actual vMark width and height var hRect = Rect(0, -2, 9, 4); // set this to the actual hMark width and height

function OnGUI(){ vRect.y = v - vRect.height - size; GUI.DrawTexture(vRect, vMark); vRect.y = v + size; GUI.DrawTexture(vRect, vMark); hRect.x = h - hRect.width - size; GUI.DrawTexture(hRect, hMark); hRect.x = h + size; GUI.DrawTexture(hRect, hMark); }

Comment
Add comment · Show 1 · 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
avatar image Quenter · Jan 14, 2012 at 01:54 AM 0
Share

Thank you for the quick help! However, I cannot get the uncert to decrease at all... I copied the method exactly, and yet when I play, the bullets go all over the place. EDIT: Fixed the problem... I just had to mess with the values of those variables. Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Object Reference not set to an instance of an object only appears occasionally? 1 Answer

How can i make a raycast in my fps game? 1 Answer

issue with bullet hole decal 1 Answer

Raycast Shooting Firerate? 1 Answer

Fps Ammo pickup problem(programming noob) 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