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 Peysbubby · Aug 18, 2015 at 02:26 PM · c#movement

How to make obect move only if its on top of another object

I am instantiating some objects in my scene so they fall onto another compound object (object made up of several parts) I want it so the objects move negatively along the z axis, but only if its touching the compound object. I have done some research on this but all I can find is raycasting to see if the object is touching the ground, however (maybe its just me because i'm not that experienced) but I cant seem to get that to work. I use c#. thanks for your help in advance.

EDIT: This is the code I am using to instantiate my objects.

void Update() {

     pennySpawn -= Time.deltaTime;
     dimeSpawn -= Time.deltaTime;
     nickelSpawn -= Time.deltaTime;

     Quaternion rotation = rotationRot;
     
     if (pennySpawn <= 0 && amountPenniesAllowed <= penniesAllowed && coins <= coinsAllowed) 
     {
         Instantiate(penny, new Vector3(Random.Range (-3.0f, 3.0f), 1.5F, Random.Range (5.0f, 15.0f)), rotation);
         pennySpawn = 1.0f;
         pennySpawn -= Time.deltaTime;
         amountPenniesAllowed = amountPenniesAllowed + 1;
         coins = coins + 1;
     }

     if (dimeSpawn <= 0 && amountDimesAllowed <= dimesAllowed && coins <= coinsAllowed) 
     {
         Instantiate(dime, new Vector3(Random.Range (-3.0f, 3.0f), 1.5F, Random.Range (5.0f, 15.0f)), rotation);
         dimeSpawn = 1.4f;
         dimeSpawn -= Time.deltaTime;
         amountDimesAllowed = amountDimesAllowed + 1;
         coins = coins + 1;
     }

     if (nickelSpawn <= 0 && amountNickelsAllowed <= nickelsAllowed && coins <= coinsAllowed) 
     {
         Instantiate(nickel, new Vector3(Random.Range (-3.0f, 3.0f), 1.5F, Random.Range (5.0f, 15.0f)), rotation);
         nickelSpawn = 1.2f;
         nickelSpawn -= Time.deltaTime;
         amountNickelsAllowed = amountNickelsAllowed + 1;
         coins = coins + 1;
     }


Comment
Add comment · Show 4
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 Runalotski · Aug 18, 2015 at 02:32 PM 1
Share

Can you post the code you are using? and you can also look up OnCollisionStay() and OnTriggerStay() wich are part of the rigidbody Physics

avatar image Peysbubby · Aug 18, 2015 at 02:40 PM 0
Share

ok I edited my post to have the script I am using to instantiate my objects in it.

avatar image Runalotski · Aug 18, 2015 at 02:43 PM 0
Share

For the locking the movment when colliding with the object you would need to add code to the movement script

avatar image Peysbubby · Aug 18, 2015 at 02:51 PM 0
Share

I understand that, and I will probably use something like this:

void Update () { transform.Translate (Vector3.back * Time.deltaTime, Space.World); }

to make my objects move. I just need to know how to call this transform.Translate to work while its on top of the other object.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kimardamina · Aug 18, 2015 at 03:12 PM

Have you tried to check if he is on the floor by checking if its colliding with the floor? And if its collides then it can move?

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 dsmeathers · Aug 18, 2015 at 02:55 PM

Add an OnCollisionStay function to a component on the coins (http://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html).

Inside OnCollisionStay do something like this:

 public Collider GroundCollider;
 public float Speed = 1.0f;

 void OnCollisionStay(Collision collisionInfo) 
 {
     if ( collisionInfo.collider == GroundCollider )
     {
         transform.position += Vector3.forward * Time.deltaTime * Speed;
     }
 }

Then in the editor set the GroundCollider property to whatever collider they're going to land on.

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

28 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Camera will not move between given points 1 Answer

Input.GetAxis not returning to zero after key is released 0 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