- Home /
How to make an object always land on one certain side?
Hi all,
I'm pretty new to Unity, so please excuse if there is an easy solution for my question I just don't know yet.
I am working on a VR project, in which objects spawn from random locations in the air, fall down to the floor, bounce off and finally stop moving and lie on the floor.
Is there a solution, for making these objects all come to lie on a certain side after the movement?
Many thanks and happy New Year!
Do you need a natural look? Basically, it's an easy task to make an object to face into a certain direction.
If you want a natural look like a cat dropped upside down with a smooth controlled turning while in air, that could be more challenging. Especially if the objects are droped dynamically and bounce off several times...
I used once a script in .js for my camera to look dynamically into a certain direction. $$anonymous$$aybe you can use that code snippet:
var TargetRotation = Quaternion.LookRotation(TargetToFace.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, TargetRotation, TurnAtSpeed * Time.deltaTime);
Answer by IgorAherne · Jan 01, 2017 at 06:30 PM
You could use Transform.Rotate() of an object from its OnTriggerEnter()
You should rotate the object over a few frames, such that transform.up is aligned with the Vector3.up (the world's up direction).
The trigger could be a spherical, and could extend 1 extra meter, beyond the object. As soon as it touches anything, your object will begin spinning to the needed orientation
You could also scale the rotation over the distance it falls relatively easy.
Thank you all very much for all the help already and your advice on the solution. $$anonymous$$aybe it's helpful, if I post as well a Screenshot of the project to specify more.
Please see below the setting. It's a cloud, out of which toasts spawn randomly. Those toasts shall all bounce off, but when they stop bouncing they shall als get to lie on one specific side.
Is it possible to achieve this with the solutions provided? Or is there maybe another script for this?
$$anonymous$$any thanks again, really appreciate your help!
Answer by salex100m · Jan 02, 2017 at 03:50 AM
HAHAHA I love this toast rain cloud idea!
Here is my solution.
If the toast lands on the wrong side, then just apply a new force to it that bounces it back up a little. Eventually every toast will land on the correct side and be stable. Maybe add a counter to your toast (if landed on wrong side then countTries++) and if countTries > 10 or something, just allow the toast to land on the bad side and then flip it over using transform rotate 180 degrees. It would actually be funny if it happens as opposed to unnatural if it happens too early.
Another way to adjust the probability of landing on one side is to think of a coin flip with a weighted coin that has more weight on one side. Add an invisible object with a heavy (more mass) rigidbody to one side of the toast and that side will fall down more likely than the other side I think.
@salex100m haha, I'm glad you like it. And a massive thanks as well for your thoughts!
I tried the second idea already, as it's easy to realize for a Beginner like me, though I think I need to experiment with this way a bit more, as toasts still land on the wrong side in too many cases.
The first solution though sounds very interesting and would be quite funny! And it sounds like a solution always bringing the right outcome :) Is that code you mentioned hard to realize? Any chance you could help me out on that one as well?
$$anonymous$$any, many thanks!
How are you moving the toast from the cloud? Are you just letting them fall due to gravity?
I assume they have rigidbodies and colliders attached right? If not, add that to the toast object. Then look up this code and use it if the toast hits the ground: rigidbody.Addforce()
Your answer
![](https://koobas.hobune.stream/wayback/20220612103823im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Place an object on the side of another object 0 Answers
How to deactivate background music when I land on an object? 1 Answer
How to move a object side to side using mouse? js3 0 Answers
Changing properties of sections of objects and take properties of objects during play? 1 Answer
Animating an object so it doesn't bounce to the initial location the animation was recorded 1 Answer