Problem with animations (newbie question)

Hi!

I try to make a game with a ball that is moving, rotating and can jump to the sides.

My question is how can I make two animations? I know about layers and AddMixingTransform but I never used them. I dont have a human body where i would need different parts to move. I just have a ball.

Atm the ball moves forward with the help of an parent object. And the ball itself has the rotation and the jumping animations but just one of them works at a time. I need the ball to rotate and then to jump when I press a button.

How can I do this?

Don’t use animations for this. Add a Rigidbody component to your sphere and then use the AddForce und AddTorque script functions to make your sphere move and spin on key presses. This looks more realistic and can be combined to create complex movements.

More info here:
https://docs.unity3d.com/Documentation/Manual/Physics.html

Thanks for the answer.

I already tried it with a rigidbody but then I had the problem that everytime it starts an animation, the position of the ball is resetet…

Can I avoid the reset?

If you insist on using animations, you shouldn’t use animations with keyframes for the position of the object.
Another solution is described here: http://forum.unity3d.com/threads/60382-SOLVED-Playing-animation-moves-gameobject-to-0-0?p=386895&viewfull=1#post386895

I don’t insist on using animations but I still don’t get how to do it.

I want my ball to move forward, rotate and jump to the sides. I tried it with two parent objects and with rigidbody. It still doesnt work.

… why/how are you using two objects? you want a ball to move, you just need to give it a physics component and add forces to it to move it.

Look up the physics sections on the Learn section.

I tried it this way:

I use: rigidbody.AddForce (Vector3.forward * 20);

The ball moves like i want it to and it also have a rotation (because its moving on the ground). But now I also want the Ball to jump to right/left. When I do the jumping with an animation it always resets the position of the ball.

Can I avoid the reset or do the jumping with another way?

Why can’t you do the jumping with additional AddForce calls?

Actually I never thought about it, but it wouldn’t work. The ball jumps between three different plattforms and if the jump isn’t as pefect as the animation-jump the ball would fall off the plattform (due to physics)

Sorry, but I have trouble understanding, what you’re trying to achieve. Could you do some kind of illustration or animation? Other platformers do use physics exclusively for their character.

In my game are three plattforms, between the plattforms is nothing. The ball starts on the middle plattform and moves forward. By pressing a key the ball can jump up, left or right. When the ball jumps left or right it lands on the left/right plattform. It works so far, the only thing that is missing is the rotation of the ball.

I still think you can do this with physics alone. You need a script that adds forces in different directions (up, left, right) depending on the key pressed. And then you can add the torque on top of that for the rotation. If you want a constant forward motion you can add a constant force.

But wouldn’t the ball fall of the plattform when it jumps by force? The ball wouldn’t stop in the middle of the plattform.

You don’t need to use gravity (at least I think that’s what you mean). If you want the ball to stop cold on hitting a platform, you can do this with a script on a collision event, that sets the velocity and angularVelocity to zero.

Really it would be easier to understand what you want with a drawing.

1513940--86111--$IMG_3069.JPG

That helps a lot and makes it a lot easier.

Create a script for the ball that reacts to three keys. Let’s call them Left, Right and Forward.
When Forward is pressed, add a force in the forward direction (z-axis in your image).
When Left is pressed, add a force to the left (-x-axis)
When Right is pressed, add a force to the right (+x-axis)

In the update function, continuously test the x value of the ball’s transform.position. When the ball collides with a platform, assign a new vector to Rigidbody.velocity with the z value set to the old forward velocity and the x and y values set to zero. This stops the ball’s sideways movement. Now use transform.position and Vector3.Lerp to move the ball to the center of the platform.

One final thing: Disable the collider of the platform, the ball is currently moving on, or it won’t be able to leave.