Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Mrhhdestroyer · May 18, 2021 at 01:58 PM · scripting problemraycastfpsshootingbullet

How to get bullets to hit crosshair

I am trying to get my bullets to shoot the crosshair and it does this instead (the continuous bullets are a feature in my game so I used it to help demonstrate where the bullets go actually go).

Here is the picture: alt text

alt text

And here's my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerWeapon : MonoBehaviour {
 
     public GameObject bulletPrefab;
 
     public Transform bulletSpawn;
 
     public float bulletSpeed = 30;
 
     public float lifeTime = 3;
     
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Fire();
         }
 
         if (Input.GetKey(KeyCode.Mouse1))
         {
             Fire();
         }
     }
 
     private void Fire()
     {
         GameObject bullet = Instantiate(bulletPrefab);
 
         Physics.IgnoreCollision(bullet.GetComponent<Collider>(), bulletSpawn.parent.GetComponent<Collider>());
 
         bullet.transform.position = bulletSpawn.position;
 
         Vector3 rotation = bullet.transform.rotation.eulerAngles;
 
         bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
 
         bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
 
         StartCoroutine(DestroyBullerAfterTime(bullet, lifeTime));
     }
 
     private IEnumerator DestroyBullerAfterTime(GameObject bullet, float delay) {
         yield return new WaitForSeconds(delay);
 
         Destroy(bullet);
         
     }
 }
 

Please help

(I think this person can help)

@azerty0220pl

screenshot-187.png (496.8 kB)
screenshot-186.png (494.2 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Llama_w_2Ls · May 19, 2021 at 08:46 AM

This code would work if your gun was facing the crosshair at all times. To do this, simply fire a raycast from the camera to the nearest wall, get the hit point, and rotate the gun to face that point. For example:

 public Transform Gun;
 public Transform cam;
 
 void Update()
 {
      RotateGun();
 }
 
 void RotateGun()
 {
      if (Physics.Raycast(cam.position, cam.forward, out RaycastHit hitInfo))
      {
           Vector3 direction = hitInfo.point - Gun.position;
           Gun.rotation = Quaternion.LookRotation(direction);
      }
 }
Comment
Add comment · Show 2 · 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 Mrhhdestroyer · May 20, 2021 at 08:29 PM 0
Share

Thanks this helped a lot but do you know how to stop the bullets from going through walls?

avatar image Llama_w_2Ls Mrhhdestroyer · May 21, 2021 at 11:22 AM 0
Share

Try the suggestion below. For bullet projectiles, just use hitscan and create a fake bullet effect either with a particle system or line renderer etc.


For larger projectiles, such as rocket launchers or grenades (or if you also want bullets with colliders), you can do a sweep test (as suggested below), and before you move the projectile forward, raycast a certain distance forward, and if it hits something, teleport it to the hit pos, as to ensure that it doesn't move through walls (if it is really fast).

avatar image
1

Answer by tuinal · May 21, 2021 at 01:12 AM

You don't typically implement bullets as physics collider objects since any physics system has issues with high velocity. Basically the engine sees the bullet on one side, then at the next physics step on the far side, and there's no frame with a collision. You can tweak rigidbody properties to continuous detection to 'sweep' ahead at each step and try to anticipate a collision, but typically for a fast projectile you raycast an instantaneous hit and create an illusion of a tracer as a visual effect, and/or delay or adjust the impact position for bullet drop etc.

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

168 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

Related Questions

Raycasr in my fps? 1 Answer

How can I shoot bullets that you can aim with the crosshair? 1 Answer

Problem with gun/shooting scripting 0 Answers

raycast spawn problem 0 Answers

How can I make a Grapple Gun work with the Character Controller? 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