Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 sayuki801 · Jul 24, 2015 at 10:44 AM · c#playerobject

Falling object when player approach

Guys any help for a falling object when player approach to the scene

Example: - player walking to a house when - suddenly a ceiling falls down to the ground.

help how to code it i have no idea how to do it. im just new in unity.

Comment
Add comment · Show 1
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 Lahzar · Jul 24, 2015 at 04:03 PM 0
Share

If the ceiling is just an object you want to fall straight down, no destruction, and you want to do it with physics: Attach a rigidbody to the object and turn of gravity. Attach a spherecollider to the player. $$anonymous$$ake it a trigger. $$anonymous$$ake a script using OnTriggerEnter to find out if the roof (use tags or layers) is inside the trigger. If it is inside the trigger, activate the gravity.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Hexer · Jul 24, 2015 at 04:45 PM

There are different way to approach this. The easiest way it probably by setting up a collider in game, when the player walks through the collider the ceilling will fall down.

     void  OnTriggerEnter(Collider other)
     {
     if(other.tag == "Player"){
     GameObject.FindWithTag("Ceilling").GetComponent<Rigidbody>().isKinematic = false;
     }

This script should be attached to the desired collider. This collider will then act as a barrier, when you walk into this barrier the code will execute the lines within the brackets. For this instance it will look if the other collider (The player) has the tag "Player" attached to it. If it does the code within that if-statement will be executed.

The code within the if-statements tells Unity to find an object with the tag "Ceilling" and will then set the isKinematic to false

(isKinematic is part of the rigidbody component)

-Things to be aware of.

  • both the objects should have a collider.(The player and the desired collider) which one of them has the isTrigger set to true.

  • The Ceilling should have a rigidbody that is set to isKinematic = true.

  • Both objects has their respective tags.

Hope this helped you a bit. If you find coding difficult, I would take a look at tutorials from Unity or on Youtube.

//LINK to Unity scripting tutorials. (I would learn C# because this is needed if you ever want to make mobile games in the future. You can't make mobile games with UnityScript because it is a dynamic scripting method.) http://unity3d.com/learn/tutorials/topics/scripting

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 sayuki801 · Jul 26, 2015 at 12:42 PM 0
Share

Ok sir. with much thanks i will try my best sir thank you again :)

avatar image sayuki801 · Jul 28, 2015 at 03:31 PM 0
Share

sir what if i want the object fall fast. becuase the object wont fall fast even though i change the drag and angular drag to 9e-19. still it fall slow? how do i make it fall fast?

avatar image Hexer · Jul 28, 2015 at 04:20 PM 0
Share

Change the drag to a lower value, like 0.1 or 0.01 If that is still not fast enough you can always attach a movement script to the object.

 using UnityEngine;
 using System.Collections;
 
 public class movement : $$anonymous$$onoBehaviour {
 
     public int movespeed = 10;
 
     void Update()
     {
     //Direction of Object (Change the keyword "down" to up
         //left or right depending on the direction you want it to go
         Vector3 Direction = Vector3.down;
         transform.Translate(Direction * movespeed * Time.deltaTime); 
     }
 }


Just be sure to disable the script if you want it to be staying on the ground without it trying to make weird movements. (with weird movements I mean, the object trying to movedownwards but it can't because there is a collider in the way.)

avatar image SmallLion · Dec 27, 2020 at 08:15 AM 0
Share

Thank u very much.After 5 years,,still useful

avatar image
0

Answer by BB77 · Oct 11, 2021 at 12:33 PM

Try this if we hit a collider the component gravityScale changes to 1 and the object starts falling

// Start is called before the first frame update void Start() {

     GetComponent<Rigidbody2D>().gravityScale = 0;

 }

 // Update is called once per frame
 void Update()
 {




 }

 void OnTriggerEnter2D(Collider2D coll)
 {
     
     string tagName = coll.gameObject.tag;
     
         if (tagName == "love")
         {
             GetComponent<Rigidbody2D>().gravityScale = 1f;


         }
             

 }

,Try this.

// Start is called before the first frame update void Start() {

     GetComponent<Rigidbody2D>().gravityScale = 0;

 }

 // Update is called once per frame
 void Update()
 {




 }

 void OnTriggerEnter2D(Collider2D coll)
 {
     
     string tagName = coll.gameObject.tag;
     
         if (tagName == "love")
         {
             GetComponent<Rigidbody2D>().gravityScale = 1f;


         }
            

 }
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

Distribute terrain in zones 3 Answers

How to use tag for all players in the Instantiate. 0 Answers

Player to Gui Object Communication in C# 1 Answer

Rotating a player object without rotating the axis 0 Answers

Multiple Cars not working 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