How to Topple a Cube over via Script - unity3d

I just want to topple a cube over front side of it. But I can't figure out how can I do this. I'm making a game with dice. It won't be rolled, just rotate 90 degrees and move forward 1 unit. How can I do it? Is there an easy way with rigidbody or should I do it all manually changing position and rotation.
By the way, sorry for bad English.

Get an empty GameObject, lets call it `flipper', and send it to the bottom edge of the cube you want to be pivoted on.
Parent the flipper to the cube.
Rotate flipper by 90 degrees.
Unparent the flipper.
Repeat.

Related

Unity 2D: Alter sprite anchor point

I've got a plain 2D Capsule Sprite. I want it to rotate around a specific point instead of its very centre. In other programs, this point is called the anchor point. For context, I'm making a paddle for my 2D pinball game so obviously the paddle needs to move when you press a button, but I can't have it moving around its very centre... Well, I could but I don't want to.
So my questions are:
Is anchor point the correct term for Unity?
Can this be altered/moved and how?
So, I ended up getting an idea from a friend who does this stuff too.
I made an empty game object. Made it the parent of my paddle. Moved the paddle to the point of the game object where I want the paddle to rotate around, and now when I rotate the game object, it rotates the paddle around the pivot point that I want it to have.
Image of paddle, arrow pointing to the empty game object
Edit: Just works using UI components within a canvas
You can use the Anchor presets of the Rect Transform. Or define the Min/Max values for x/y coordinates in Anchors Property.

How to implement trolley-like physics behaviour in Unity

I am stuck with two things while trying to simulate the physics setup for a trolley-like object (push powered vehicle with free rotating wheels on the front and fixed wheels on the back).
I have the RigidBody with its mesh and four WheelColliders, the object moves fine if I just apply torque to the wheels. However if I use the AddForce method on the RigidBody it won't move; I see the object being pushed (slightly balancing) but the wheels won't rotate so it stays in place. How can I get the wheels to move if the object is being pushed?
My other problem is to simulate the standard 360 degrees rotating wheels on the front of the trolley. What would be the best way to simulate this? I was thinking about a horizontal WheelCollider and a vertical one as a child but that seems really weird and I doubt it will actually work. Any ideas?
https://docs.unity3d.com/Manual/WheelColliderTutorial.html
This tutorial shows an example of how to use the wheel colliders and apply steering and torque to them.
It might be what you are looking for in regards to the adding force part.
I suspect it might allow the 360 degree rotation as well, but I am not familiar enough with these colliders to guarantee that.
EDIT:
In the guide there is the line:
public float maxSteeringAngle; // maximum steer angle the wheel can have
Which looks relevant for the 360 degree turning.

How can I rotate an object based on an angle?

I am working in Unity3D and I was wondering how I would rotate a cube based on the angle between the cube and the mouse position. With that I don't mean the mouse position in world space but in pixels.
Here are some pages that'll lead you to your answer:
Input.mousePosition This also includes an example of how to turn screen coordinates into a ray in world coordinates. If you know how far away from the camera you want your point, check out ScreenToWorldPoint for a point instead of a ray.
transform.Rotate To perform a rotation.
The rest of your question is kinda vague--rotating "based on" the angle between cube and mouse position could mean a lot of things. You shouldn't need much more information than this though!

How to make Unity3d move objects on a table?

I am working on a tabletop game that user controls using accelerometer in the phone. It is pretty similar to Labyrinth on iOS.
The scene contains a tabletop created using planes and cubes, and a sphere as a ball. As it works in Labyrinth game, on tilting phone ball should drop to the tilted side, while the camera stays centered to table. I am trying to do similar thing where user tilts the phone, and objects on table move to tilted side.
Currently, I add the x and z component to Physics.gravity on tilt. Sadly, this change in gravity does not affect the ball which stays put on the table. Use gravity is selected for the ball, and it drop down from height to the tabletop initially and then comes to halt. After the initial drop, ball does not react to any gravity change.
I have also tried rotating the whole table, but using transform.rotate does not work either. The table rotates perfectly, alongwith the camera, but the ball stays put hanging in the air. So, currently I am out of my depth about the issue.
Is there any other way to allow tilt action registered, so that ball moves to the tilted direction? I cannot use addforce function, as there are multiple objects which need to react to tilt, and it will be difficult to keep track of that many addforce calls. How else can I get it working?
See this post for some related information.
As for why your sphere is sticking to the table, can I assume your sphere has a rigidbody? If so, you need need to wake it up:
if (rigidbody.IsSleeping())rigidbody.WakeUp();
Ideally you would make that call only after detecting a change in orientation/gravity, not every frame.

RigidBody and collision.contacts

I'm trying to re-invent the wheel once again for a 2.5D platformer.
I tried some things with the CharacterController but it only send the "sides" info, not if it's coliding specifically left or right.
So now I'm working with RigidBody and the OnCollisionEnter() function
So thing is, all points returned by the collision.contacts seems to have some priorities. Here is what I have in game
the green box is the BoxColider
the red cubes are contact points from collision.contacts returned on collision from the RigidBody
As you can see, eveb if I touch a wall, all the contacts points are on the "ground" side of the Box.
If I jump :
I have point where I need them (here left side) because in air. But once I touch the ground I lose all left/right points from the returned array. All points go back to what we see in the first screenshot.
And so I have nothing to know when the Box is touched on the sides (and specifically left or right).
Any idea ? or better methods ?
Thanks !
I think you could try to do your stuff with
OnCollisionStay()
to get all the contacts points.
from http://unity3d.com/support/documentation/ScriptReference/CharacterController.html