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 /
avatar image
0
Question by PlatPlayz · Jan 12, 2018 at 07:28 PM · gravityfallingmark

Mark on the ground for falling object in Unity3D

Hello, I wanted to make an object fall from the sky to the ground. I already have the object spawning and falling, but I wanted to have a mark on the ground so the player knows where the object is falling. Is there a way to do that?

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 Harinezumi · Jan 15, 2018 at 02:19 PM

[EDIT] Check the comments for more info!

There is a dirty way to do it, and a more general, cleaner way.

Dirty way
I did this when I was first learning Unity and the free version did not yet have shadows. Create a prefab with the marker you want to show on the ground, and Instantiate() it at the same xz position as your falling object, but very slightly above your ground (i.e. GameObject markerInstance = Instantiate(markerPrefab, new Vector3(fallingObjectPosition.x, 0.05f, fallingObjectPosition.z), Quaternion.Identity); ). The only issue with this is that if the falling object gets Destroyed(), the marker will remain. So you can assign the markerInstance to some variable of a script on the falling object, and in the OnDestroy() of that script do if (marker != null) { Destroy(marker); }

Cleaner way
Your falling object will need a script that controls the creation of your marker, e.g. FallMarkerControl. FallMarkerControl needs a GameObject fallMarkerPerfab field, to which you need to assign the prefab for a generic fall marker object (I would write [SerializeField] private GameObject fallMarkerPrefab = null;, but this is a personal preference). Then in Start() you cast a ray from your falling object downwards (or more general, in your "fall" direction), and the position that you hit is where you need to position a new instance of the fallMarkerPrefab.
Something like this:

 [SerializeField] private GameObject fallMarkerPrefab = null;

 [SerializeField] private  Vector3 fallDirection = Vector3.down;

 private GameObject fallMarkerInstance = null;
 
 private void Start () {
     if (fallMarkerPrefab != null) {
         RaycastHit hit;
         if (Physics.Raycast(new Ray(transform.position, fallDirection), out hit)) {
             Vector3 fallPosition = hit.point;
             fallMarkerInstance = Instantiate(fallMarkerPrefab, fallPosition - 0.05f * fallDirection, Quaternion.identity); // small displacement to avoid z-fighting with the ground
         }
     }

     private void OnDestroy () {
         if (fallMarkerInstance != null) { Destroy(fallMarkerInstance); }
     }
 }

Note that this second way also only works correctly if you have a more or less flat ground, otherwise your marker will float (of course, that can be solved if you rotate your fallMarkerInstance with the normal of the raycast hit).

Hope this helps!

Comment
Add comment · Show 5 · 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 PlatPlayz · Jan 15, 2018 at 02:24 PM 1
Share

What if the ray hits the player or any other object? Will it instantiate on top of it?

avatar image Harinezumi PlatPlayz · Jan 15, 2018 at 03:16 PM 0
Share

Yes, if it hits the player or an object different from the ground, then it will be created on top of that object. To avoid that, you can put the ground on a different layer, and then use the layer checking version of Physics.Raycast() (which is actually more efficient). See this answer for how it can be done: https://answers.unity.com/questions/1108781/set-ray-only-when-raycast-a-specific-layer.html

avatar image PlatPlayz Harinezumi · Jan 15, 2018 at 06:03 PM 0
Share

Even though I use int ...= Layer$$anonymous$$ask.Get$$anonymous$$ask(""); nothing seems to change.

Show more comments
avatar image
0

Answer by kaiguy720 · Jan 12, 2018 at 11:01 PM

@PlatPlayz Is the surface to be marked flat?

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 PlatPlayz · Jan 13, 2018 at 05:21 AM 0
Share

Yes, it is flat and it's y is 0.

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

75 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

Related Questions

character gravity problem 1 Answer

Endless "Falling in place" Simulation Question 0 Answers

Freeze constrains bug or idk 0 Answers

My object is falling through the platform 1 Answer

Why is my GameObject just falling in space after creation? 4 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