- Home /
How to make objects fall?
I am currently trying to code somewhat of a type of physics where a part of a tree will fall if it is not on top of a solid object using C#. This is what I have in the Script:
using UnityEngine; using System.Collections;
public class TreeBlockScript : MonoBehaviour { public Transform ground; public int fallSpeed = 1; private Transform treeTransform; void Awake() { treeTransform = transform; }
// Use this for initialization void Start () { } // Update is called once per frame void Update () { GameObject go = GameObject.FindGameObjectWithTag("solid_Block"); ground = go.transform; Debug.DrawLine(ground.position, treeTransform.position, Color.red); if(Vector3.Distance(ground.position, treeTransform.position) >= 0) { treeTransform.position += treeTransform.up fallSpeed Time.deltaTime; } else { fallSpeed = 0; } } } The problem that I am running in to is that it selects only one of the tagged blocks and it is very far away, rather than directly under it, so it always falls. Picture is of a Debug showing all of the objects targeting a single block far away from it. Is there a way to get around this? I tired GameObject[] go = GameObject.FindObjectsWithTag{"solid_Block") but it doesn't seem to work with transforms. Thank you for your time. (Oh yeah, plus all of the objects are falling as you can see. I don't know if that is a problem with the targeting or just the whole script.)
Jmane2012,
Could you do us a favour and format your code using the code styles? Helping us to help you helps everybody ;)
Answer by aldonaletto · Sep 11, 2012 at 01:04 AM
I can't understand what do you expect from this code: it finds an object tagged "solid_Block" (any one), measures the distance to the owner object (the one the script is attached to) and always move the owner object in its up direction, since Vector3.Distance is always >= 0!
Why don't you simply use rigidbodies? That's what they are for: falling things, bouncing things, colliding things etc. You don't even need any script to do that: just pile the objects, and they will fall if there's nothing below. If you want to have some "non-solid" blocks, don't add rigidbodies to them and set their Is Trigger checkbox.
Anyway, if you want an object to fall when there's no "solid_block" below it without using rigidbodies, do a short raycast downwards and check which's the tag of the object: if there's nothing below, or if it's not a "solid_block", move the object down.
using UnityEngine; using System.Collections;
public class TreeBlockScript : MonoBehaviour {
public float fallSpeed = 1; public float margin = 0.1f;
private Transform treeTrf; private float range;
void Awake() { treeTrf = transform; }
void Start () { // find the distance to its bottom plus the margin: range = treeTrf.collider.bounds.extents.y + margin; }
void Update () { RaycastHit hit; // check if something is inside the range below this object: if (Physics.Raycast(treeTrf.position, Vector3.down, out hit, range){ // if it's a "solid_Block" object, return without move the object: if (hit.transform.tag == "solid_Block") return; } // otherwise move it downwards at fallSpeed: treeTrf.Translate(Vector3.down fallSpeed Time.deltaTime); } }