- Home /
How to make obect move only if its on top of another object
I am instantiating some objects in my scene so they fall onto another compound object (object made up of several parts) I want it so the objects move negatively along the z axis, but only if its touching the compound object. I have done some research on this but all I can find is raycasting to see if the object is touching the ground, however (maybe its just me because i'm not that experienced) but I cant seem to get that to work. I use c#. thanks for your help in advance.
EDIT: This is the code I am using to instantiate my objects.
void Update() {
pennySpawn -= Time.deltaTime;
dimeSpawn -= Time.deltaTime;
nickelSpawn -= Time.deltaTime;
Quaternion rotation = rotationRot;
if (pennySpawn <= 0 && amountPenniesAllowed <= penniesAllowed && coins <= coinsAllowed)
{
Instantiate(penny, new Vector3(Random.Range (-3.0f, 3.0f), 1.5F, Random.Range (5.0f, 15.0f)), rotation);
pennySpawn = 1.0f;
pennySpawn -= Time.deltaTime;
amountPenniesAllowed = amountPenniesAllowed + 1;
coins = coins + 1;
}
if (dimeSpawn <= 0 && amountDimesAllowed <= dimesAllowed && coins <= coinsAllowed)
{
Instantiate(dime, new Vector3(Random.Range (-3.0f, 3.0f), 1.5F, Random.Range (5.0f, 15.0f)), rotation);
dimeSpawn = 1.4f;
dimeSpawn -= Time.deltaTime;
amountDimesAllowed = amountDimesAllowed + 1;
coins = coins + 1;
}
if (nickelSpawn <= 0 && amountNickelsAllowed <= nickelsAllowed && coins <= coinsAllowed)
{
Instantiate(nickel, new Vector3(Random.Range (-3.0f, 3.0f), 1.5F, Random.Range (5.0f, 15.0f)), rotation);
nickelSpawn = 1.2f;
nickelSpawn -= Time.deltaTime;
amountNickelsAllowed = amountNickelsAllowed + 1;
coins = coins + 1;
}
Can you post the code you are using? and you can also look up OnCollisionStay() and OnTriggerStay() wich are part of the rigidbody Physics
ok I edited my post to have the script I am using to instantiate my objects in it.
For the locking the movment when colliding with the object you would need to add code to the movement script
I understand that, and I will probably use something like this:
void Update () { transform.Translate (Vector3.back * Time.deltaTime, Space.World); }
to make my objects move. I just need to know how to call this transform.Translate to work while its on top of the other object.
Answer by kimardamina · Aug 18, 2015 at 03:12 PM
Have you tried to check if he is on the floor by checking if its colliding with the floor? And if its collides then it can move?
Answer by dsmeathers · Aug 18, 2015 at 02:55 PM
Add an OnCollisionStay function to a component on the coins (http://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html).
Inside OnCollisionStay do something like this:
public Collider GroundCollider;
public float Speed = 1.0f;
void OnCollisionStay(Collision collisionInfo)
{
if ( collisionInfo.collider == GroundCollider )
{
transform.position += Vector3.forward * Time.deltaTime * Speed;
}
}
Then in the editor set the GroundCollider property to whatever collider they're going to land on.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Camera will not move between given points 1 Answer
Input.GetAxis not returning to zero after key is released 0 Answers