Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 romans8710 · Apr 08, 2020 at 09:53 AM · scripting problemfpsshootinggungun script

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

I just implemented a shooting mechanism into my gun where bullets are created and move out of the barrel of the gun forwards and since they come from the barrel, their direction doesn't match the crosshair in the middle of the screen. How can I make the bullets move so you can aim with the crosshair? here is my script:

/*

private void ShootGun() {

     GameObject bullet = Instantiate(bulletModel, gunPos.position, gunPos.rotation);
     bullet.AddComponent(typeof(Rigidbody));

     Rigidbody rb = bullet.GetComponent<Rigidbody>();
     rb.useGravity = false;

     rb.velocity = camera.forward * speed;


 }

*/

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
1

Answer by azerty0220pl · Apr 09, 2020 at 12:15 PM

Hello @romans8710 You can solve it using raycasting:

 //Create a ray. Syntax: Physics.Raycast(startPosition, direction, hitInfo)
 Physics.Raycast(yourCamera.transform.position, yourCamera.transform.forward, RaycastHit hitInfo);
 
 //the code you wrote now
 bullet.AddComponent(typeof(Rigidbody));
 Rigidbody rb = bullet.GetComponent<Rigidbody>();
 rb.useGravity = false;
 
 //Rotate the bullet towards the point where the ray hits a collider
 bullet.transform.LookAt(hitInfo.point);
 
 //It is better to add a force, not to modify velocity.
 rb.AddForce(bullet.transform.forward * speed, ForceMode.VelocityChange);
 

It can have a weird effect if you are close to the target, but should work. If you want to learn more about raycasting, there is a video on YouTube made by Unity about it.

Comment
Add comment · Show 5 · 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 Aduis · May 29, 2020 at 12:16 AM 0
Share

What is hitInfo, it comes up as a an error

avatar image Aduis · May 29, 2020 at 03:36 AM 0
Share

What is the hit info? I am really new this especially raycasting.

avatar image SteenPetersen Aduis · May 29, 2020 at 03:41 AM 0
Share

'hit info' is the object that is returned of type Raycast from shooting a ray. You can query this object for information about for example where it hit, how long it is away etc. As you can see from @azerty0220pl answer he/she first creates a Raycast called 'hitInfo' in this line:

  //Create a ray. Syntax: Physics.Raycast(startPosition, direction, hitInfo)
  Physics.Raycast(yourCamera.transform.position, yourCamera.transform.forward, RaycastHit hitInfo);


And then refers to the point where it hit in this line:

  //Rotate the bullet towards the point where the ray hits a collider
  bullet.transform.LookAt(hitInfo.point);


for more information, I suggest using unity learn. In particular something like this.

avatar image Aduis SteenPetersen · May 29, 2020 at 02:20 PM 1
Share

So what would I have to do for it to not be an error? I made it a RaycastHit variable and its till come up as an error.

avatar image Yoshirouuu · Jul 15, 2021 at 07:10 PM 0
Share

@Aduis This may not be relevant anymore to you but just so people here aren't as confused when they see this solution (which is a good one), the syntax for the Raycast would be like follows RaycastHit hitInfo; Physics.Raycast(yourCamera.transform.position, yourCamera.transform.forward, out hitInfo); The error you're getting is because it needs to be an output of "RaycastHit" variable type. Hope this helps!

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

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

FPS recoil 1 Answer

How to add sound to gun shot script 4 Answers

GetButtonDown problem | C# 0 Answers

How would I add force/ burst fire to my gun? 2 Answers

Shooting script problem. 2 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