- Home /
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.
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.
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
Ok sir. with much thanks i will try my best sir thank you again :)
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?
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.)
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;
}
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rotating a player object without rotating the axis 0 Answers
Player to Gui Object Communication in C# 1 Answer
Distribute terrain in zones 3 Answers
how to spawn object on the player C# 2 Answers