Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 fraubolat · May 30, 2020 at 10:06 AM · scripting problemraycastshootingbullets

shooting raycast

every thing is working fine but its instantiating the bullet around the player and not shooting it here the script using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.SocialPlatforms;

 public class shootingforshutgun : MonoBehaviour
 {
     public Transform ponit;
     public float Speed =10f;
     public GameObject bullet;
     bool canshot = false;
     int Bullets = 10;
     int currentbullet;
     bool reloading = false;
     int range = 100;
     int howmanytimes = 8;
     void Start()
     {
         currentbullet = Bullets;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if(Input.GetButtonDown("Fire1"))
         {           
            shoot();
             
         }
         Debug.Log(currentbullet);
     }
 
 
     public void shoot()
     {
         
         float x = Random.Range(-10f, 10f);
         float y = Random.Range(-10f, 10f);
         Vector3 a = new Vector3(x , y);
         Vector3 c = transform.forward + a;
         if (!canshot)
         {
             RaycastHit hit;
             if (Physics.Raycast(ponit.position,c , out hit, range))
             {
                 Instantiate(bullet, hit.point, Quaternion.LookRotation(hit.normal));
 
 
             }
 
         }
         currentbullet--;                    
 
         
     }
 }   
 
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

Answer by KoenigX3 · May 30, 2020 at 10:37 AM

Does bullet have a script to manage its movement? If not, then you need to put a RigidBody on it, and you must access it after instantiating.

 GameObject bullet = Instantiate(bullet, hit.point, Quaternion.LookRotation(hit.normal));
 bullet.GetComponent<RigidBody>().AddForce(bullet.tranform.forward * 100, ForceMode.Impulse);


You can of course use a variable instead of 100. Also, make sure that the bullet prefab is looking at the forward axis, otherwise it won't go forward. Keep in mind that if you are using this method to create collisions, you need to change the RigidBody's collision detection mode if the bullet is too fast. You can read about this on the UnityDocs website.

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 fraubolat · May 30, 2020 at 11:12 AM 0
Share

ok thank you it worked but why did you store the Instantiate method in a variable?

avatar image KoenigX3 fraubolat · May 30, 2020 at 01:31 PM 0
Share

I stored the GameObject in a temporary variable called bullet so i can get the RigidBody component. If you check Instantiate, it returns the instantiated GameObject so you can optionally store it and use it if you need to (to set it inactive, or for other reasons). The variable itself is local, so it gets discarded after it is out of scope.

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

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

Related Questions

change to raycast shooting instead of rigidbody shooting 1 Answer

My Projectile System has Another Bug 1 Answer

Bullet speed changes depending on how close and far I click the mouse button 2 Answers

scripting problem 0 Answers

Multiplayer FPS Bullets: should they be rigid bodies or raycast? 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