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
0
Question by aronatvw · Jan 22, 2014 at 04:47 AM · instantiateraycastmousevelocity

raycast and instantiat advice

I am about to start production on a new game where the code below is one of the main functions. I learned how to write it from a tutorial and just came back to it. I made a little tech. demo works nicely the raycast finds a the object you click with mouse and instantiates a bullet from another object.

My only worry is when I click on objects further away from the inatantiated prefab the velocity seems to be much greater than closer objects! A little insight would be very welcome before starting! Thank you in advance!

 if(Input.GetMouseButtonDown(0)){
             
             Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hitinfo;
     
             
             if(Physics.Raycast(rayOrigin, out hitinfo, distance)){
                 
                 // Var for third person raycasting
                  // Also part of third person raycasting.
                 var direction = (hitinfo.point - actor.transform.position);
                 // var testray = new Ray(actor.transform.position, direction);
                 // Draws ray from 'actor' var to mouse click.
                 // Seems to draw multiple rays at the moment. I think from the camera.
                 Debug.DrawRay(actor.transform.position, direction);
                 
                 Debug.Log("You are casting a ray");
                 Debug.DrawLine(rayOrigin.direction, hitinfo.point);
                 
                 // Instantiate.
                 Rigidbody clone;
                  
                 clone = Instantiate(bullet, actor.transform.position, transform.rotation) as Rigidbody;
                 clone.velocity = transform.TransformDirection(direction * speed);
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 Benproductions1 · Jan 22, 2014 at 04:57 AM 1
Share

you need to normalize your direction vector. Otherwise it will have the magnitude of the distance between the two points it was calculated from.

avatar image aronatvw · Jan 22, 2014 at 05:00 AM 0
Share

after looking at it I realized since the distance I am shooting becomes greater this multiplys by the speed. I am shooting in the x direction so I guess if I subtract that value of direction from speed it would make it a little better?

avatar image robertbu · Jan 22, 2014 at 05:52 AM 1
Share

@aronatvw - The issue you outline in your comment is the same one that @Benproductions1 addresses with the need to 'normalize your direction vector:

 var direction = (hitinfo.point - actor.transform.position).normalzied;
avatar image aronatvw · Jan 22, 2014 at 06:13 PM 0
Share

awesome guys thank you I've never heard of that. I am going to try it. Also I will debug.log and look and the script reference but any other insight before hand on what it actually does?

avatar image aronatvw · Jan 22, 2014 at 06:33 PM 0
Share

Thank you guys that worked out perfectly. So from what I understand any "distance" between the two points is set to one. Regardless it works perfectly thank you much!

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

19 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

Related Questions

Instantiating Object On Map 1 Answer

How to prevent GameObject from spawning on top of each other? 1 Answer

Instantiate Objects on Cells created during run time 1 Answer

Instantiate a little before the end of the raycast or on screen 0 Answers

Raycast from Character to Mouse Position? 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