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 ncp12 · May 10, 2021 at 11:13 PM · unity 5fpsraycasting

Raycasting

I'm making an fps and I just decided to start with a sniper. Of course, it being a sniper, I used raycasts for the bullets. However, when the sniper is not zooming in, it doesn't have a very accurate raycast.

Here's the code:

variables: public float range = 100f public int bulletsPerMag = 3 public float fireRate = 2f public int currentBullets = 3 float fireTimer public Transform shotPoint

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 void Start(){
     currentBullets = bulletsPerMag;
 }
 void Update()
 {
     if (Input.GetButton("Fire1")){
         Fire();
     }
     if (fireTimer < fireRate){
         fireTimer += Time.deltaTime;
     }
 }
 private void Fire()
 {
     if(fireTimer < fireRate) return;    

     RaycastHit hit;

     if(Physics.Raycast(shotPoint.position, shotPoint.transform.forward, out hit, range)){
         Debug.Log(hit.transform.name + " got pewed");
     }

     currentBullets--;
     fireTimer = 0.0f;  //reset fireTimer


 }


For example, when I aim like this: alt text

it tells me that I am hitting the floor.

Please help me! Thanks in advance!!

fps-thomas-collab-home-pc-mac-linux-standalone-uni.png (411.2 kB)
Comment
Add comment · Show 3
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 ncp12 · May 10, 2021 at 11:14 PM 0
Share

ignore the name of the project lol

avatar image GSGregory · May 11, 2021 at 12:16 AM 0
Share

So. Currently you are firing a ray from where the bullet leaves the gun? It seems like the problem is that in that position shotPoint.transform.forward is down. Instead of doing it simply from the object. Try something like this:

https://answers.unity.com/questions/590671/direct-a-raycast-from-point-a-to-point-b.html https://gamedev.stackexchange.com/questions/169933/raycasting-from-an-object-to-the-center-of-the-screen-crosshair-without-clippi

Where you end up raycasting to the crosshair point.

avatar image ncp12 GSGregory · May 11, 2021 at 05:14 PM 0
Share

Thanks! I'll try it out.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ncp12 · May 11, 2021 at 06:29 PM

It now detects the red capsules, but doesn't detect the floor.

using System.Collections; using System.Collections.Generic; using UnityEngine;

 void Start(){
     currentBullets = bulletsPerMag;
 }
 void Update()
 {
     if (Input.GetButton("Fire1")){
         Fire();
     }
     if (fireTimer < fireRate){
         fireTimer += Time.deltaTime;
     }
     
 }
 private void Fire()
 {
     // if(fireTimer < fireRate) return;    

     // RaycastHit hit;

     // if(Physics.Raycast(shotPoint.position, shotPoint.transform.forward, out hit, range)){
     //     Debug.Log(hit.transform.name + " got pewed");
     // }

     // currentBullets--;
     fireTimer = 0.0f;  //reset fireTimer

     Vector3 GetHitPoint(Vector3 muzzlePosition) {

         Ray crosshair = new Ray(camera.position, camera.forward);


         Vector3 aimPoint;
         RaycastHit hit;
         if(Physics.Raycast(crosshair, out hit, range)) {
             aimPoint = hit.point;        
         } else {
             aimPoint = crosshair.origin + crosshair.direction * range;
         }

         Ray beam = new Ray(muzzlePosition, aimPoint - muzzlePosition);


         if(!Physics.Raycast(beam, out hit, range ))
             return aimPoint;
             Debug.Log(aimPoint);


         Debug.Log(hit.transform.name + " got pewed");
         return hit.point;
         
     }
     GetHitPoint(muzzlePosition);


 }

}

I know I wont be hitting the floor, but in case there is an error in the code, or something..Anyone know what's happening?

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

247 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird FPS Camera 0 Answers

Problem with constant Unaccounted fps reducer. 1 Answer

WebGl Mouse Look Problem 0 Answers

Displaying the total number of enemies & updating the number of enemies remaining as they're killed. 1 Answer

Why does FPS drop when switching from Unity 5.3.4 to 5.4.2? 0 Answers


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