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 $$anonymous$$ · Sep 15, 2016 at 01:31 PM · c#gameobjectplayerplacement

Need help with GameObject placement

What I'm trying to do here is to place a GameObject seed on the ground infront of the player on the ground, but I get weird results; When I jump, it gets placed in the air and so on..

How can I get it to always be placed on the terrain infront of the player / where the player is looking?

What I tried was to subtract 1 from playerPosition.y and add 1 to playerPosition.z but that didn't help much at all.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class PlaceSeeds : MonoBehaviour {
     
     // Use Player GameObject
     public GameObject player;
 
     // Use a seed model
     public GameObject seedToPlaceModel;
 
     private Vector3 objectSpawnPosition;
     private Vector3 playerPosition;
     
     // Update is called once per frame
     void Update () {
         playerPosition = player.transform.position;
         objectSpawnPosition = new Vector3 (playerPosition.x, playerPosition.y, playerPosition.z);
 
         // If we leftclick (Mousebutton)
         if (Input.GetKeyDown (KeyCode.Mouse0)) {
             Instantiate (seedToPlaceModel, objectSpawnPosition, Quaternion.identity);
         }
     }
 }
 


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 ThomLaurent · Sep 15, 2016 at 02:03 PM

What you are looking for is the Physics.Raycast() function ( see doc ) :

 Physics.Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

To be used like this:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 float maxDistance = 100; // in Unity units
 
 if (Physics.Raycast(ray, out hit, maxDistance))
 {
     Debug.Log("The mouse hit a collider at " + hit.point);
     // Draw a line to see the ray
     Debug.DrawLine(ray.origin, hit.point);
 }


Note you can use the layerMask parameter to only hit terrains instances by placing each of your terrains in a specific layer

Comment
Add comment · Show 6 · 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 $$anonymous$$ · Sep 15, 2016 at 02:51 PM 0
Share

Hi, thanks for the guidance. I got it somewhat working, I checked all layers that are not used except "Terrain" layer, but now all trees spawn in the exact same position.

It also didn't spawn infront of me when placing them.

Code:

 using UnityEngine;
 using System.Collections;
 
 public class PlaceSeeds : $$anonymous$$onoBehaviour {
 
     // The range of the weapon
     public float placementRange;
 
     // Debug
     public bool debugRay = false;
 
     // Use the Camera FPSCamera
     public Camera FPSCamera;
 
     public Layer$$anonymous$$ask checkIgnoreLayers;
 
     // Use a seed model
     public GameObject seedToPlace$$anonymous$$odel;
 
     private Vector3 objectSpawnPosition;
     private Vector3 playerPosition;
     
     // Update is called once per frame
     void Update () {
         // Ray in the middle of the screen, centered
         Ray ray = FPSCamera.ScreenPointToRay (Input.mousePosition);
         RaycastHit hitInfo;
 
         objectSpawnPosition = new Vector3 (playerPosition.x, playerPosition.y, playerPosition.z);
 
         // If we leftclick ($$anonymous$$ousebutton)
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.$$anonymous$$ouse0)) {
             if (Physics.Raycast (ray, out hitInfo, placementRange, checkIgnoreLayers, QueryTriggerInteraction.UseGlobal)) {
                 
                 // Instantiate (Spawn) the seedToPlace$$anonymous$$odel to terrain (checkIgnoreLayers) only
                 Instantiate (seedToPlace$$anonymous$$odel, objectSpawnPosition, Quaternion.identity);
 
                 // What GameObject the seed spawned
                 Debug.Log ("Player planted a " + seedToPlace$$anonymous$$odel.name);
             }
         }
     }
 }

avatar image ThomLaurent $$anonymous$$ · Sep 15, 2016 at 03:01 PM 0
Share

That's normal, in your code you set your object position to objectSpawnPosition which is a copy of playerPosition. Since it's a private field I suppose it runs Vector3.zero

avatar image $$anonymous$$ ThomLaurent · Sep 15, 2016 at 03:36 PM 0
Share

Oh, you're right. I missed that.

I removed playerPosition variable and switched that part in the Update() function to say:

 objectSpawnPosition = transform.position;

It sorta works. The GameObject spawn floating in the air and "ignores" the terrain. I doublechecked the layers being ignored and it's still correct as only Terrain layer is checked.

avatar image ThomLaurent $$anonymous$$ · Sep 15, 2016 at 03:13 PM 0
Share

I think I now understand what you're ai$$anonymous$$g for, does this code works for you?

 public class PlaceSeeds : $$anonymous$$onoBehaviour {
 
     // The range of the weapon
     public float placementRange;
 
     // Use the Camera FPSCamera
     public Camera FPSCamera;
 
     public Layer$$anonymous$$ask checkIgnoreLayers;
 
     // Use a seed model
     public GameObject seedToPlace$$anonymous$$odel;
 
     // Update is called once per frame
     void Update () {
         // Ray in the middle of the screen, centered
         Vector3 direction = new Vector3(0, -1, placementRange);
         Ray ray = new Ray(FPSCamera.transform.position, FPSCamera.transform.rotation * direction);
         RaycastHit hitInfo;
         
         // If we leftclick ($$anonymous$$ousebutton)
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)) {
             if (Physics.Raycast(ray, out hitInfo, direction.magnitude, checkIgnoreLayers, QueryTriggerInteraction.UseGlobal)) {
 
                 // Instantiate (Spawn) the seedToPlace$$anonymous$$odel to terrain (checkIgnoreLayers) only
                 Instantiate(seedToPlace$$anonymous$$odel, hitInfo.point, Quaternion.identity);
 
                 // What GameObject the seed spawned
                 Debug.Log("Player planted a " + seedToPlace$$anonymous$$odel.name);
             }
         }
     }
 }
avatar image $$anonymous$$ ThomLaurent · Sep 15, 2016 at 03:47 PM 0
Share

It is! This is exactly what I was trying to do, thank you @ThomLaurent! :)

Show more comments
avatar image
0

Answer by DanielArkham · Sep 15, 2016 at 02:06 PM

First, are you making a FPS game, or something like that? If that is the case, what you can do is to create an empty object, attach it to the Camera of the FPS Player Prefab and put the object in front of the camera.

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

223 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 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

How to Parent a Camera to the Head Bone with No Camera Shake? 0 Answers

Place GameObject on terrain even if player is looking up 2 Answers

Status Effect Help 0 Answers

how do i get my player get sucked into a gameobject 2 Answers

Launch Pad Issues 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