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
1
Question by jcv8000 · Jul 31, 2014 at 03:43 AM · camerajavascriptpoint

How to add force towards where the camera is pointing?

I'm trying to make a weapon fire but when I'm firing, it is shooting towards a certain point in the map, and if I move at an angle (holding W and A at the same time) it shoots towards the direction I'm walking.

I just need a thing to insert here to make it go towards where the camera (player) is pointed

 bullet.rigidbody.AddForce (<THINGY HERE> * 20);


Also I'm using js to do this.

Comment
Add comment · Show 5
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 robertbu · Jul 31, 2014 at 03:45 AM 0
Share

This is not a simple problem of inserting 'thingy here'. Unless you are shooting directly from the center of the screen and have gravity turned off, you will need to triangulate and/or compensate. That is, where you point the weapon will depend on at what distance you want the projectile to cross the center of the view of the camera, and if you want to do any compensation for the fact that a projectile falls due to gravity.

avatar image jcv8000 · Jul 31, 2014 at 12:42 PM 0
Share

I just need something to put there as a direction where the camera is looking (center of the screen), like Vector3.forward (That's an example direction I can't use that).

avatar image VesuvianPrime · Jul 31, 2014 at 12:44 PM 0
Share

You can use camera.transform.forward

avatar image robertbu · Jul 31, 2014 at 01:49 PM 0
Share

Take a look at this diagram.

alt text

The hex is the camera. The triangle is where you are shooting/moving from. So to be able to shoot/go where the camera is looking, you have to answer the question at what distance. All the dots represent places where the camera is looking, but the object moving/shooting has to select one of them. If you know the distance, you can do something like this:

  var point = Camera.main.transform.position + Camera.main.transform.forward * 10;
  var dir = (point - bullet.transfrom.position).normalized;
  bullet.AddForce(dir * 500);

This will shoot where the camera is looking 10 units from the camera.

avatar image jcv8000 · Jul 31, 2014 at 02:10 PM 0
Share

robertbu, when I try that it spawns them in this one direction by me, and if I run forward it it shoots them in front of me, but not standing still.

VesuvianPrime, when I do that it works, but I can only shoot them in a 180 degree half-circle. So if I spin 360 degrees while shooting, it shoots where I'm looking for half of the spin. The other half it shoots them on the edge of that 180 degree half-circle it will shoot them from.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by QC_Scott · Jul 31, 2014 at 02:08 PM

If you use the transform of the camera instead of the bullet's transform for applying force it should work. A script like this should work.

public GameObject Camera; public GameObject Bullet;

OnMouseDown(){ Bullet.rigidbody.AddForce(Camera.Transform.Forward * 10); }

Then you simply use the inspector to reference the bullet object and camera object. Hope this was helpful.

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 jcv8000 · Aug 05, 2014 at 02:05 AM 0
Share

Still only shoots in that 180 degree half-circle.

avatar image spliterr jcv8000 · Mar 21, 2019 at 03:09 AM 0
Share

Hi, I know this is too late, but i have faced the problem today, in case you still need the answer why it only shoots correctly on the half circle, it is because of the "player" collider. If you set the spawn position the same position as the camera, when you look down, the projectile will hit the player collider you can fix this by either disabling collider of projectile or simply move the spawn position a litte bit further in front of the camera, i hope this helps anyone having this problem

avatar image
0

Answer by shock2provide · Jul 31, 2014 at 02:07 PM

Try Camera.main.transform.forward

Comment
Add comment · Show 1 · 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 jcv8000 · Aug 05, 2014 at 02:05 AM 0
Share

Still only shoots in that 180 degree half-circle.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Camera to follow the mouse to move to create the point 0 Answers

Help with understanding and modifying a camera script (JavaScript) 1 Answer

Camera follows not right 0 Answers

How to tell if an object is right next to another? 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