Move a 3d rigidbody to its facing direction in Unity3d - unity3d

I have the following scene:
I want the robot to move to each facing direction, so I tried:
rigidbody.AddForce(rigidbody.transform.TransformDirection(Vector3.up* speed));
And the robot is moving up like jumping.
I tried:
rigidbody.AddForce(rigidbody.transform.TransformDirection(Vector3.forward* speed));
but the robot is falling down
Any idea how can I solve this?

He is most likely falling down because he is falling over due to Physics.
You can give his Rigidbody rotation constraints to freeze his rotation in X and Z axes - so he can still turn to face a direction, but he cannot fall over. The use your "forward" code to move him.
These constraints can also be accessed from code, so you can for example turn them off if he is killed, so he falls over:
rigidbody.constraints = RigidbodyConstraints.None;

Related

[Solved - 3D movement on X and Y with mesh colliders

I am working on an Asteroids clone in 3D with top-down camera. This setup is static and will not change (so no, I do not want to transform the project to 2D game instead).
This means that I need to limit all movement to X and Y axis. I created movement for both asteroids and player and everything worked fine. All movements are done using AddForce on respective RigidBody components.
The problem is then I start dealing with collisions. I use Mesh Collider components to get a nice and precise "touch reaction". The problem is that when collision like this occurs, the new movement vector has Z value different from 0. This is a problem as the object will start moving on Z axis.
What have I tried:
Freezing constraints on RigidBody
Manully reseting Z in Update function
The first solution (freezing constraints) did not work and neither did the second one (moreover, the second one seems quite messy)
So the question is
What would be the optimal way to force physics-based movement only to X and Y axis while using precise collision with Mesh Colliders?
are you sure you used the position restriction correctly? You can check to set the restrictions with a vector as in the documentation. https://docs.unity3d.com/ScriptReference/RigidbodyConstraints.FreezePosition.html
to see how its done. If not please share the code or a screenshot of the rigidbody restrictions you tried in the editor

Physics material Friction works only in forward direction - used with wheel joint 2D

I have created a car vehicle using 2D sprite, and connected circular wheel sprites to it using Wheel joint 2D. In controller script, When movement variable is non- zero i am creating instance of motor and enabling it.
if (movement == 0f) {
backWheel.useMotor = false;
} else {
backWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D{ motorSpeed = movement, maxMotorTorque = backWheel.motor.maxMotorTorque };
backWheel.motor = motor;
}
I am controlling direction by setting movement variable +ve or -ve. It is working perfectly in forward direction. However when i press left, on a moving car, it stops but than tire slips in backward direction , as if there were no friction. I have added physics material to both tire and ground, and behavior in forward direction is normal ( no slips).
is there friction only in one direction ? Or am i missing something here ?
Okay I found what was wrong. Well, late night work has its pro and cons. cons mostly.
Anyhow, direction friction made no sense. In case you noticed I am applying motor to back wheel only. So when I was pressing brakes(changing direction) it was loosing touch with the ground. Thus slippery.
I could have never noticed this behavior in case I had applied motor to both wheels, but that wasn't required of my vehicle behaviour.
I solved the problem by adding more weight to back wheel, so that it was always in touch with the ground. Thus no slips.

Unity: fixed Y axis of prefabs

I am building a top down shooter using the unity survival shooter assets. The problem I face is that all of the spawnable prefabs(enemy, player) have a fixed Y axis position. This is sometimes causing them to float in places where the ground level is low and sometimes they will sink where the ground level is higher. They do not change or update the Y axis as the terrain does.
How can I fix this so that the prefabs move according to the terrain.
Enemy Settings
PlayerSettings
Scene Screenshot
Terrain Navmesh
I would recomand you read this: http://answers.unity3d.com/questions/197952/rigid-body-and-character-controller.html if this doesn't solve your problem,
first I would try to play around with the mass and angular drag. Second of all I would rebake the nav Mesh. Third I would change the slope limit and step height and rebake again. And last I would try to use agent.updateposition = true; and agent.updateRotation = true;
I hope at least one worked. Happy coding !

Unity: Ball falls through object that is rotating around another object

Imagine you have a ball falling due to gravity. When it encounters a rotating "cube" object, then you would expect it to bounce off of the object. However, in my case if the cube is rotating fast, the ball goes through it, but if the cube is rotating slowly, the ball hits it and bounces away as expected.
I am using RotateAround() inside the Update() method to achieve the "cube" object's rotation. I tried setting the ball's collision detection to Discrete, Continuous, and Continuous Dynamic with no luck.
My goal is to make the ball bounce away no matter how fast the "cube" object is rotating around another object.
Is there something else I should look into?
Thanks!
You can try lowering the Fixed Timestep value under Edit > Project Settings > Time.
Be aware that this will affect the performance of the game as you're calculating physics more often.
Documentation: http://docs.unity3d.com/Manual/class-TimeManager.html
Also, I assume that you have box and sphere colliders as opposed to mesh colliders? The former are more efficient at detecting collisions.

Unity physics about collision and energy

I have been working on a Unity ping pong game using the Leap Motion. I use rigidbody.MovePosition() to move the paddle. However, when I hit the ball (which uses gravity), the paddle launches it too far. Even when I change the masses of both, it doesn't do anything.
What variable should I change to reduce this energy going into the ball?
From the following link.
"MovePosition will put your object at the target location, no matter what. It may push aside other objects in a realistic way, or may blast them out of the way, or may just pass right through them. But it will gladly move you through a solid wall or a mountain.
If you're using MovePosition on a rigidbody to add from where you currently are, it looks like AddForce. With AddForce, the physics step does all the work: applies your velocity, sees the collision and handles the bounce. With MovePosition, the physics step sees you're mysteriously overlapping a solid object. If it isn't too much, it will bounce you apart."
You won't need to use MovePosition. Instead, you can figure out the direction of the shot (based on the position of the ball relative to the paddle). Then you can add an impulse force in that direction to the ball.
Pseudo-code (from the following link):
Vector3 shootDir = ballPosition - paddlePosition; // Calculate direction of the shot
shootDir.Normalize(); // Normalize the shot vector
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Credit due to Owen Reynolds and Tim Michels.