Hi, I am learning/discovering Unity a bit by creating a little game with a ball character and till know everything works great. Everything is turning out much easier then I thought it would be. But there is one thing I don’t really know how to solve.
I am using the code below to make te ball roll into the direction the camera is pointing. Which works great but here it comes. The ball has a texture with a face on it and I want it to move into the direction the ball is going. At the moment the face is going everywhere.
var speed:float = 10;
var jump:float = 5000;
var skitmark:Transform;
private var jumping = false;
function Update()
{
var cameraTransform:Transform = Camera.main.transform;
var verticalSpeed:float = Input.GetAxis ("Vertical") * speed;
var horizontalSpeed:float = (Input.GetAxis ("Horizontal") * speed)*-1;
if(!jumping)
{
rigidbody.AddTorque(cameraTransform.right*verticalSpeed);
}
else
{
rigidbody.AddForce(cameraTransform.forward*verticalSpeed);
}
if(!jumping Input.GetButtonUp("Jump"))
{
jumping = true;
rigidbody.AddForce(Vector3.up * jump);
}
}
function OnCollisionEnter(collision:Collision){
jumping = false;
}
Is my question clear enough? I don’t want een complete script or something. I just want a tip, trick or idea from a Unity Guru (or newbie if he knows an answer).
The face is going everywhere because the ball is using a full physics simulation. From what I understand of your question, you want the face to point in the direction the ball is going.
What you’ll first want to do is separate the mesh and collider (That is, your player controller object is your collider+rigidbody, and you put the mesh in a child object to that controller). Then freeze rotation on the collider+rigidbody. Freeze Rotation freezes physics input so you’ll need to use scripted rotation instead of torque to control where the mesh looks at. Movement shouldn’t be affected.
If your question was how to stop the ball’s rotation to use it as a character with the face pointing forward at all times, this should do the trick.
However, if you wanted to have control over how the ball rotates, you can now animate the child object (the ball mesh) independantly and control a bit more how it behaves. For example, allow rotation to a single axis when moving forward/backwards.
In that case, you’ll need to communicate the controller’s forward velocity to determine how fast to rotate the mesh; multiply rotation speed by the forward velocity, so when you move backwards (negative vector) the ball rotates backwards too. Tweak variables as needed to get the desired effect.
I hope this helps.
Thank’s, for the detailed reply this is exactly what I needed.
It worked right away, thanks alot Werewraith!!!
You can also let the ball roll freely and have a projector with the face texture pointing at its front at all times. You’d have to set it up so that the projector ignored all layers but the ball layer, but that could work if you want the ball to look like it’s rolling.
you can also check “freeze rotation” on the rigidbody