Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 sarahfoojn · Oct 25, 2021 at 08:41 AM · raycastraycasthit

Raycast always hitting the same place even if I click different areas?

void Update() { RaycastHit hit; if (cm.glareOn == true) { if (Input.GetMouseButtonDown(0)) {

             GameObject bullet = Instantiate(lightbullet, Input.mousePosition, Quaternion.identity);

             if (Physics.Raycast(origin.transform.position, origin.transform.forward, out hit))
             {
                 Debug.Log(hit.transform.position);
                 bullet.transform.position = Vector3.MoveTowards(origin.transform.position, hit.transform.position, speedbullet * Time.deltaTime);
             }
         }
     }
 }
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
Best Answer

Answer by Hellium · Oct 25, 2021 at 09:15 AM

hit.transform is returns the Transform of the object hit by thee raycast, not the raycast impact point

              bullet.transform.position = Vector3.MoveTowards(origin.transform.position, hit.point, speedbullet * Time.deltaTime);


Several other notes about your code:


  • Instantiate(lightbullet, Input.mousePosition, Quaternion.identity);. Input.mousePosition is a screen-space position, not a world space position. You most likely want to spawn the bullet at the barrel's position (maybe origin.transform.position?)


  • Moving the bullet once under Physics.Raycast does not really make sense. You most likely want your bullet to travel on its own over time. You'll find several examples on the web to help you with that.

Comment
Add comment · Show 11 · 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 sarahfoojn · Oct 25, 2021 at 09:48 AM 0
Share

@Hellium thank you but the hitpoint doesnt change either

avatar image Hellium sarahfoojn · Oct 25, 2021 at 09:53 AM 0
Share

Then, please explain the actual issue you are facing. Images may help.

Have you solved the two other issues I've mentioned?

avatar image sarahfoojn Hellium · Oct 25, 2021 at 10:45 AM 0
Share

So basically I managed to get it to shoot now, thank you for your suggestion :) but it still only shoots in one direction no matter where I click. alt text it only shoots in that direction and I can't get it to change. Also I made a new script for the bullet void Update() { transform.position = Vector3.MoveTowards(transform.position, am.target, 30 * Time.deltaTime); }

so it is shooting properly now but still only to one point and not the point i am clicking

I am sorry if it is unclear and thank you for all your help!

screenshot-23.png (186.1 kB)
Show more comments
avatar image
0

Answer by AwptiK · Oct 25, 2021 at 11:34 AM

Perhaps do it something like:

 bullet.transform.position += new Vector3(0, 0, speed);

Or:

 bullet.transform.position = Vecto3.Lerp(transform.position, hit.point, speed);

where speed is how fast the bullet moves. Put these in FixedUpdate(). If you want them in Update() multiply speed by Time.DeltaTime.

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 sarahfoojn · Oct 25, 2021 at 12:19 PM 0
Share

thank you, but i think my problem is that the raycaster only hits one area. the object in front of the camera has a box collider but it still can't seem to hit it(it hits the back plane) and the hit point doesn't change no matter what ,:)

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

184 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

Related Questions

Raycast Coding Issues 1 Answer

Mesh polygon of a hit surface on a gameobject 1 Answer

Detect when object is no longer hit by Raycast 2 Answers

Can't find a clone object with RayCast? [Solved] 2 Answers

ray cast returning the wrong object, going through multiple objects 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