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 thenachotech1113 · Feb 10, 2014 at 11:46 PM · raycast

how do i spawn an object x units from a hit in the racyastHit normal's direction

hello everyone, i am not sure how understandable is the title of this question. anywhay ill do my best to explain a little better. i want to instantiate an object the way the diagram below sort of represents:

 |../
 |./ <--- raycast 
 |/
 |----O <---(object)
 |
 |<--- surface hit
 

i want to know what is the Vector3 at which the object spawns, i know how to get the normal of the hit and the position at which it hit, but what i do not know is how to get the vector3 where the object would be spawned, and that is what i dont know how to get.

i want to let you know i do not want you guys to write any code for me other than maybe some exaple, but if possible to explain how would you do this.

thank you all very much in advanced.

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
2
Best Answer

Answer by clunk47 · Feb 11, 2014 at 12:29 AM

In this C# Example, I attach the script to the main Camera. For testing, I placed some cubes in random positions somewhere in front of the camera. Each time you click on a cube in the scene, this will instantiate your assigned prefab halfway between your Camera and the object you're clicking on.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     Ray ray;
     RaycastHit hit;
     public GameObject prefab;
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown (0))
         {
             GameObject clone = Instantiate(prefab, Vector3.Lerp(transform.position, hit.transform.position, 0.5F), hit.transform.rotation) as GameObject;
         }
     }
 }
Comment
Add comment · 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
0

Answer by highpockets · Feb 11, 2014 at 12:19 AM

Well if you have the position at which you send the raycast from. For example, if you were to send it from the camera, you store that position:

(Assuming the script is on the camera)

 Vector3 castPos = transform.position

And you said you have the normal position. So just get the difference of the 2 Vectors and divide by 2:

 Vector3 diff = castPos - normalPos;
 Vector3 targetPos = diff / 2; //instantiate here
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 highpockets · Feb 11, 2014 at 07:18 AM 0
Share

Sorry my calculation won't work!! Oops.. I was thinking that you could simply use Vector3.Lerp though:

 Vector3.Lerp( castPos, normalPos, 0.5f );

That should return the halfway Vector3 position that you want to instantiate at. The Lerp function will give you the castPos at 0.0f and the normalPos at 1, so, 0 - 1 is like 0-100% of the distance

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

20 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

Related Questions

Health System Raycast Bullets 2 Answers

Perspective issue with railgun/raycasting 0 Answers

Prevent Raycast on reacting to GUI input? 2 Answers

Why Raycast is not working properly? 0 Answers

Confused as to how my RaycastHit script works 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