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 Imaginaire · Sep 01, 2012 at 06:42 PM · instantiateraycastpositionmouse

Instantiating Object On Map

Before I start with the question, I already searched the forum for a solution, but was unable to get any to work for me.

Basically, I have an overhead camera looking down at my map from an angle. What I want, is to Instantiate the object on the map wherever I click.

Right now my script is this

 #pragma strict
 
 
 var object : GameObject;
 var distanceAway : float;   
 var rayHit : RaycastHit;    
 var a : Vector3;   
 
 function Start () {  
 }
 
 function Update () {  
  
     if(Input.GetMouseButtonDown(0)) { 
         if(Physics.Raycast(transform.position, Vector3.down, rayHit)) {
             distanceAway = rayHit.distance;
             Debug.Log("rayhit");
         }


     
         a = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,distanceAway));

         Instantiate(object, a, Quaternion.identity);
     }
 }

The code does instantiate an object somewhat where I want it to, but it is at the wrong height. It's because the ray distance is from my camera to the ground, and not from my mouse cursor to the ground, if that makes any sense. I can't seem to find a solution to get the object to instantiate at the same height as the ground. Any help would be greatly appreciated!

Update: I've been experimenting further and find, as long as my camera is facing directly at the ground (a 90 angle directly at the ground) and the terrain below me is flat, then the objects instantiate on the terrain right. The problem is I need to have my camera at an angle, and somehow compensate for that when I instantiate.

Update: Wow... I have spent literally 20+ hours researching this subject, experimenting, trying code, etc.. Then while looking into casting rays according to local rotation I just happen to stumble across the answer.. haha weird how things work out.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by THEpancakes · Sep 01, 2012 at 06:52 PM

Not sure about this, as I haven't tried this out in Unity yet, but perhaps you could simply use "rayHit.point" for the coordinates to instantiate your object...?

I'll check if this works and edit my comment...

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 Imaginaire · Sep 01, 2012 at 09:15 PM 0
Share

I've been playing around with it a bit and I can't seem to get it to correspond with where I click. If anyone else has had any more luck, I'd appreciate it since I have been working on this issue for three days now... ha

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

8 People are following this question.

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

Related Questions

Fire object towards mouse cursor 1 Answer

Can't Instantiate an object at the clicked position.,I can't Instantiate an Object at the clicked position. I am trying to make a minecraft game. 1 Answer

Instantiate a prefab at the Mouse's position? 1 Answer

How to set the mouse x and/or z position as a variable 1 Answer

Help with moving instantiated object's transform 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