Problem with moving an imported animation

Hello! :slight_smile: It’s my first post here so I hope I’ll do good :stuck_out_tongue:

I have a pretty simple task to do for my college class - a game, that I figured out be a simple third person platformer with a cat as a main character. Currently I’m having a big problem with moving my cat the right way. This is how a part of my CatMovement script looks like:

void FixedUpdate () {

   		walkSpeed = Input.GetAxis ("Vertical");
		turnSpeed = Input.GetAxis ("Horizontal");
		if (walkSpeed < 0) 
		{
			transform.Translate (0, 0, 0);
		} 
		else 
		{
			transform.Translate (0, 0, walkSpeed * 0.1f);
		}
		transform.Rotate (0, turnSpeed, 0);
		if(!Input.GetKey("left") && !Input.GetKey("right"))
		{
			action = false;
			animator.SetBool ("akcja", action);
		}
		else{
			action = true;
			animator.SetBool ("akcja", action);
		}
		animator.SetFloat ("chodzenie", walkSpeed);
		animator.SetFloat ("obrot", turnSpeed);
		if (transform.position.y < -2)
		{
			Die ();
		}
	}

It’s able to rotate at a slow speed, but whenever I click W or UP arrow - instead of going forward it goes to the right. In the FixedUpdate script I’m also changing values for an Animator, cause I want the walking animation to run then the speed goes up.

Can anyone suggest me what can I do about it? Maybe a different way to steer the cat? I tried using RigidBody, but whenever I start the game - the cat disappears. I tried exporting my cat model in a few different ways from 3ds MAX but nothing works. I’ve read the tip about creating an EmptyObj to be the parent of an imported object, but I don’t really know how would it work with an animation, mostly because I have to move the character (I want it to be third-person, so the cat in moving in the direction it’s facing.) and animate it. Maybe writing a script for the EmptyObj just for the movement, and a script for the cat to change the animator values, check for collisions etc?

Any help would be appreciated. And also sorry if my english isn’t that good, it’s not my native language :wink:

There are two things to change:

Rotation:

When you execute this line in your FixedUpdate() it is being called a lot of times while you press your key:

transform.Rotate (0, turnSpeed, 0);

So, in case you want to rotate your character in increments do this, which will rotate exactly turnSpeed each second you have your key pressed:

transform.Rotate (0, turnSpeed * Time.deltaTime, 0);

But if what you want is to turn your character to one way or another do this instead:

transform.rotation = Quaternion.AngleAxis(90, rotationAxis);

That will turn your character exactly 90 degrees, no matter how many times it is called.

Translation:

Do not move your player like this as it will ignore all physics calculations along the way:

transform.Translate (0, 0, walkSpeed * 0.1f);

Instead do it like this:

Rigidbody2D rigidBodyPlayer = GetComponent<Rigidbody2D> ();
Vector3 movement = new Vector3(0, 0, walkSpeed * Time.deltaTime);
rigidBodyPlayer.MovePosition (transform.position + movement);

If you are using 3D physics then use Rigidbody instead of Rigidbody2D.