Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Inferi · Sep 01, 2016 at 01:31 PM · c#gameobjectplacement

Place GameObject on terrain even if player is looking up

Hi I am trying to place a GameObject infront of the player and on the ground "terrain".

I have this code in my Update():

 if (Input.GetMouseButtonDown(1)) { // Högerknappen på musen
             Vector3 playerPosition = transform.position;
             Vector3 playerDirection = transform.forward;
             Quaternion playerRotation = transform.rotation;
             float spawnDistance = 1.0f;
             Vector3 spawnPos = playerPosition + playerDirection * spawnDistance;
 
 
             GameObject insObj;
             insObj = Instantiate(waterExtractor, spawnPos,Quaternion.identity) as GameObject;
 
             RaycastHit hit;
             Vector3 ray = insObj.transform.TransformDirection(Vector3.down);
             Physics.Raycast(insObj.transform.position, ray,out hit);
             insObj.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             insObj.transform.localPosition = new Vector3(insObj.transform.localPosition.x, ((insObj.transform.localPosition.y - hit.distance)), insObj.transform.localPosition.z);
         }

This works great as long as I dont look upwards. Then the object is placed up in the air. What am I doing wrong here? Please check the small clip on youtube for demostration of my problem : Placement problem

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

Answer by Justinger · Sep 01, 2016 at 02:22 PM

Looking over your code, this is what I think is happening. Unless you have some kind of collider in your sky, your raycast should not be returning a hit. So, you have instantiated an object and later try to modify it's position with an empty value, so it sits there. I would recommend starting with the raycast, and checking if there is a hit, like:

  if (Physics.Raycast(ray, out hit))  

and then doing an instantiate with the hit.point as the position.

 Instantiate(waterExtractor, hit.point,Quaternion.identity) as GameObject;

I hope this helps.

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 Inferi · Sep 01, 2016 at 03:17 PM 0
Share

Thanks for the info, I will try and report back :)

avatar image
0

Answer by Inferi · Sep 01, 2016 at 07:20 PM

@Justinger : I have now changed my code and it works now :) My code now is:

         if (Input.GetMouseButtonDown(1)) { // Högerknappen på musen
             Vector3 playerPosition = transform.position;
             Vector3 playerDirection = transform.forward;
             Quaternion playerRotation = transform.rotation;
             float spawnDistance = 1.0f;
             Vector3 spawnPos = playerPosition + playerDirection * spawnDistance;
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             RaycastHit hit;
             if (Physics.Raycast(transform.position, fwd, out hit, 5.0f)) {
                 if (hit.transform.GetComponent<Collider>().name == "Terrain") {
                     GameObject insObj;
                     insObj = Instantiate(waterExtractor, spawnPos, Quaternion.identity) as GameObject;
                     insObj.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
                     RaycastHit hit2;
                     if (Physics.Raycast(insObj.transform.position, Vector3.down, out hit2)) {
                         insObj.transform.localPosition = new Vector3(insObj.transform.localPosition.x, ((insObj.transform.localPosition.y - hit2.distance)), insObj.transform.localPosition.z);
 
                     }
                 }
             }
 


I dont know if it was how you was thinking. Now I will just clean up the code and check so the player cant put another object up on the first one :) Thanks for the help

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with GameObject placement 2 Answers

Can not access GetComponentInParent ( ). usegravity 1 Answer

Error CS1519 1 Answer

Animation won't stop (Solved) 2 Answers

The referenced script on the behaviour is missing error help plz 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