Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jmane2012 · Sep 10, 2012 at 09:08 PM · physicsfalling

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. alt text 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.)

debug.jpg (55.6 kB)
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 kristercollin · Sep 10, 2012 at 11:50 PM 0
Share

Jmane2012,

Could you do us a favour and format your code using the code styles? Helping us to help you helps everybody ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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); } }

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

10 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

Related Questions

Multiple Cars not working 1 Answer

how to make an object fall slowly ? 2 Answers

Having trouble with Physics.IgnoreCollision(). 1 Answer

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 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