Reputation: 1
Okay so I am working on a platformer in unity 2D. I have some animations for a character with a player controller attached (punch, kick and block, which work fine). When I move, my player controller calculates his velocity and depending on its value, if he is not jumping or blocking/kicking/punching he will be in the 'walk' or 'run' states (if not idle). Basically this just means his limbs rotate at a certain speed (the limbs are children of the player, who is a parent).
However, this simple task is turning out to be hard. I originally had the following script to make the limbs rotate:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour {
private int b;
void FixedUpdate () {
b = PlayerController.b;
float y = transform.localEulerAngles.z;
transform.RotateAround (transform.position, Vector3.forward,
b*45*Mathf.Sin(3*Time.fixedTime)-y);
}
}
(Ignore the 'b').
This worked fine until I turned the character to the left, when the rotations started going haywire.
I decided to simplify and use the following script to rotate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour {
float y;
void Start(){
y = transform.localRotation.z;
Debug.Log (y);
}
void FixedUpdate () {
float u = y + 45 * Mathf.Sin (3 * Time.fixedTime);
Quaternion rot = transform.localRotation;
rot.z = u;
Debug.Log (u);
transform.localRotation = rot;
}
}
How can this go wrong? This is not even rotating at all. I have tried debugging the angle and have noticed that the angle (of the limb I am looking at) changes for a few seconds (even though I can't see any rotation), then gets stuck at a certain value (usually about 45). What is happening? Am I missing something?
tl;dr I just want to have his limbs oscillate with time.
Upvotes: 0
Views: 167
Reputation: 21
I think its best you make an animation for walking, and the way you can play it is you can have a variable like bool isWalking
, and then you say
if(isWalking){ animator.play(walkAnimation); }
As you can see there is a variable called 'animator' in mine. You should search up tutorials about the unity animator, those are very helpful. Hopefully this helps, if it does not I'm so sorry
Upvotes: 1