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 Mrkrisher · Nov 08, 2016 at 01:06 AM · objectdelayfallingfalling-off-moving-thing

How to make objects fall when the player gets close

I have a game where you roll a ball around, collect yellow cubes, and avoid obstacles. I want to know how to add objects to fall when the player gets close.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Gosta · Nov 09, 2016 at 01:45 AM

This requires that IsKinematic is off, since "If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore", and gravity is a force.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))] //Make sure a rigidbody is attached to the falling object
 public class fallingScript : MonoBehaviour {
 
     public Transform player; //This creates a slot in the inspector where you can add your player
     public float maxDistance = 5f; //This can be changed in the inspector to your liking
     private Rigidbody rigidbody;
     void Start () {
         rigidbody = GetComponent<Rigidbody> ();
         rigidbody.useGravity = false;
     }
 
     void Update(){
         if (Vector3.Distance (player.position, transform.position) < maxDistance) { //transform is the object that this script is attached to
             rigidbody.useGravity = true;
         }
     }
 }


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 Mrkrisher · Nov 09, 2016 at 08:22 PM 0
Share

Thank you.

avatar image
1

Answer by Filhanteraren · Nov 08, 2016 at 07:36 AM

Set the falling object rigidbody to "Is kinematic".

Add a trigger that changes the isKinematic to false when the player enters it.

     public Rigidbody FallingObject;

     public void OnTriggerEnter(Collider other)
     {
         if (FallingObject.isKinematic)
             FallingObject.isKinematic = false;
     }

Comment
Add comment · Show 4 · 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 Mrkrisher · Nov 08, 2016 at 10:18 PM 0
Share

Error $$anonymous$$essage. Any idea on what I should do?

alt text

screen-shot-2016-11-08-at-51734-pm.png (33.3 kB)
avatar image Filhanteraren Mrkrisher · Nov 08, 2016 at 11:08 PM 0
Share

Is the code inside a class?

avatar image Mrkrisher Filhanteraren · Nov 09, 2016 at 01:13 AM 0
Share

I created a new script for it, and I can't attach it to anything because there is something wrong with the script.

Show more comments
avatar image
0

Answer by jmgek · Nov 08, 2016 at 03:01 AM

There are many different ways (raycast, trigger, distance) but this is a start: https://www.youtube.com/watch?v=CjXlC-rrdDc

There are lots of resources out there.

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 EIQUY3 · Nov 08, 2016 at 07:36 AM

You can make an array of all the objects you want to check with player, then you can check distance from player in a for loop. GameObject[] GameObjectArray; for (int i = 0; i < GameObjectArray.Length; i++ ) { float distance = Vector3.Distance(yourPlayer.transform.position,GameObjectArray[i].transform.position); // YourPlayer is name for your player if (distance < 1f) // choose your distance here { // Your Logic here for making objects fall } }

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

64 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

Related Questions

Falling through the floor issue 11 Answers

Cycle Textures Over Time 2 Answers

TriggerEnter, useGravity, Cubes are not falling down 3 Answers

Touch Lag With S2 1 Answer

Trigger Falling Object 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