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 /
  • Help Room /
avatar image
0
Question by DrewCarter101 · Apr 20, 2017 at 04:46 PM · gunprojectilefirefirst person shootercannon

STUCK! Firing a simple sphere from below the camera at crosshair in the centre of the screen, with or without raycast.

I have a game that spawns "enemies" within a sphere around the camera, the player moves the camera to target and simply presses the screen to fire. For now my "ammo" is simply a sphere and for visual purposes the object I'm firing from is a sphere located below the camera. the "ammo" should fire from the "cannon" below the camera directly towards the crosshair and hit whatever target is in the crosshair.

With some online help this is how my crosshair is generated:

 if(Time.timeScale != 0)
     {
         if(crosshairTexture!=null)
             GUI.DrawTexture(new Rect((Screen.width-crosshairTexture.width*crosshairScale)/2 ,(Screen.height-crosshairTexture.height*crosshairScale)/2, crosshairTexture.width*crosshairScale, crosshairTexture.height*crosshairScale),crosshairTexture);
         else
             Debug.Log("No crosshair texture set in the Inspector");
     }


And currently this is how my "Cannon" works

if ( Input.GetButton("Fire1") && Time.time > mNextFire){

         mNextFire = Time.time + mFireRate;

         Rigidbody projectile= (Instantiate (ammo, gameObject.transform.position, Quaternion.identity)).GetComponent<Rigidbody>();
         projectile.AddForce (projectile.transform.forward * mHitForce);
     }

Then on the ammo, I have this:

   int x = Screen.width / 2;
   int y = Screen.height / 2;
   transform.LookAt(new Vector3(x, y));

but it is horribly inaccurate, I had to move the canon around so it appears to be firing from below the camera, the speed/velocity is off and it barely hits what's being aimed at. It also often appears to go through the cross hair but doesn't collide with the target. I know it's not the target cause if I move the canon around a bit and keep the target still, it hits, the target takes damage, life bar decreases and the "ammo"that hit it "disappears".

I know questions like this are all over the internet however none of them describe my problem, and the ones I found somewhat similar have no solutions. After days of frustration I decided to ask the question here, I just started unity last week so I apologise in advance if my problem seems silly.

Also, I've been seeing a lot of things with raycast, I found this script that projected a yellow laser towards the centre of the screen;

 void Update()
 {
     int x = Screen.width / 2;
     int y = Screen.height / 2;
 
     Ray ray = camera.ScreenPointToRay(new Vector3(x, y));
 
     Debug.DrawRay(ray.origin, ray.direction * 1000, new Color(1f,0.922f,0.016f,1f));
 
 }

it worked perfectly and projected the laser right to the centre but what am I supposed to do with it afterwards? how do I make the projectile follow it? I'd really appreciate the help. Thank you!

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

0 Replies

· Add your reply
  • Sort: 

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

99 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

Related Questions

Adding muzzle flash to the gun.... 1 Answer

Ammo Script 0 Answers

Having trouble getting fps gun(s) too shoot please help me out now 2 Answers

Walk animation wont play but idle will 0 Answers

Projectiles 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