- Home /
Unity character rigging controls
Hi all I I have a question I have a character that walks runs and jumps with the standard wasd keys and jumps with spacebar. I would like him to transform when pressing a button and keep that animation and not move to another. I know its being canceled with the current code that I have and I can't seem to stop it. Here is my code
using UnityEngine; using System.Collections;
public class PlayerRobot : MonoBehaviour { //public GameObject player; public float speed = 3.0f; public float jumpSpeed = 150.0f;
public float flightspeed = 10.0F;
public float flightrotationSpeed = 100.0F;
Rigidbody rigidbody;
Animation anim;
Animation animStop;
bool isJetMode = false;
bool isRobotMode = true;
public float h = 0;
public float v= 0;
bool idleAnim = true;
bool jumpAnim = true;
bool runAnim = true;
bool strafeleftAnim = true;
bool straferightAnim = true;
bool transformAnim = true;
bool walkAnim = true;
// Use this for initialization
void Awake()
{
rigidbody = GetComponent<Rigidbody> ();
anim = GetComponent<Animation> ();
}
// Update is called once per frame
void FixedUpdate ()
{
RobotMove ();
JetMode ();
Jump ();
VulcanAnim ();
h = Input.GetAxis ("Horizontal");
v = Input.GetAxis ("Vertical");
}
void Jump()
{
if (Input.GetButtonDown ("Jump"))
{
GetComponent<Animation>().Play("jump");
rigidbody.AddForce (Vector3.up * jumpSpeed);
}
}
void JetMode()
{
if (Input.GetMouseButtonDown (1))
{
GetComponent<Animation> ().Stop ("idle");
anim.Play ("transform");
rigidbody.AddForce (Vector3.up * jumpSpeed);
rigidbody.mass = 1;
rigidbody.drag = 0;
rigidbody.useGravity = false;
isJetMode = true;
isRobotMode = false;
print ("Gravity is turned off ");
}
if (Input.GetMouseButtonUp (1))
{
GetComponent<Animation> ().Stop ("idle");
anim.Play ("transform");
rigidbody.AddForce (Vector3.down * jumpSpeed);
rigidbody.mass = 3;
rigidbody.drag = 0;
rigidbody.useGravity = true;
isJetMode = false;
isRobotMode = true;
print ("Gravity is turned on ");
}
}
void RobotMove()
{
float translation = Input.GetAxis("Vertical") * flightspeed;
float rotation = Input.GetAxis("Horizontal") * flightrotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
void VulcanAnim()
{
if (Input.GetAxis ("Vertical") >= 1.0f) //forward
{
GetComponent<Animation> ().Stop ("idle");
anim.CrossFade ("run", 0.2f);
}
if (Input.GetAxis ("Vertical") <= -1.0f) //backwards
{
GetComponent<Animation> ().Stop ("idle");
anim ["run"].speed = -1.0f; // reverse run anim
print ("Runningbackward");
}
else if (Input.GetAxis ("Horizontal") <= -1.0f)
{
GetComponent<Animation> ().Stop ("idle");
anim.CrossFade ("strafeLeft", 0.2f);
}
else if (Input.GetAxis ("Horizontal") >= 1.0f)
{
GetComponent<Animation> ().Stop ("idle");
anim.CrossFade ("strafeRight", 0.2f);
}
else if (Input.GetMouseButtonDown (1))
{
anim.CrossFade ("transform", 0.2f);
GetComponent<Animation> ().Stop ("idle");
}
else
{
anim.CrossFade ("idle", 0.2f);
}
}
void idleEnded()
{
idleAnim = false;
}
void jumpEnded()
{
jumpAnim = false;
}
void runEnded()
{
runAnim = false;
}
void strafeleftEnded()
{
strafeleftAnim = false;
}
void straferightEnded()
{
straferightAnim = false;
}
void transformEnded()
{
transformAnim = false;
}
void walkEnded()
{
walkAnim = false;
}
}
Answer by sisse008 · Dec 13, 2016 at 01:23 AM
if your character moves to another animation after finishing current one than its because of the hierarchy in the Animator Controller. can you show us a screen shot of it?
Answer by Numonic · Dec 13, 2016 at 04:09 PM
@ Sisse008 Sure but I've just yesterday tried using the Animator in Unity which I was using the Legacy. Figured I try the new one out and see if I can get better results cause of it's robust nature and power. I did get a better result but it's not exactly how I would like it. You see the character runs, idles, jumps and transforms.
The character will have more animations but these are the most important and transformation seems to give me most of the heartache. I have the character transform and lift up and be in Jetmode and when I press the button again to switch animations he doesn't come down fast enough, which i know is a rigidbody issue and just a simple setting, but as soon as he lands he stays in jetmode and doesn't return fast enough to robot mode. Also thanks for the reply. :) I also revamped the character controller code to tailer more of the Animator in Unity. Which by the way is absolutely awesome. Here is the updated code
using UnityEngine; using System.Collections;
public class PlayerAnimatorTest : MonoBehaviour {
public float flightspeed = 6.0F;
public float flightrotationSpeed = 100.0F;
public float speed = 60.0f;
public float jumpSpeed = 700.0f;
public AudioClip robotTransforming = null;
public AudioClip robotFootsteps = null;
Animator anim;
Rigidbody rigidbody;
// Use this for initialization
void Awake ()
{
anim = GetComponent<Animator> ();
rigidbody = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void FixedUpdate ()
{
RobotMove ();
Jump ();
RobotMode ();
}
void RobotMove()
{
float translation = Input.GetAxis("Vertical") * flightspeed;
float rotation = Input.GetAxis("Horizontal") * flightrotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
if (translation != 0) //forwards and backwards movement with anim
{
anim.SetBool ("isRunning", true);
} else
{
anim.SetBool ("isRunning", false);
}
}
void Jump()
{
if (Input.GetButtonDown ("Jump"))
{
anim.SetTrigger ("isJumping");
rigidbody.AddForce (Vector3.up * jumpSpeed);
}
}
void RobotMode()
{
////////MouseInput//////////////////////////////////////////
if (Input.GetMouseButtonDown (1))
{
anim.SetBool ("isTransform", true);
anim.SetBool ("isIdle", false);
rigidbody.AddForce (Vector3.up * jumpSpeed);
rigidbody.mass = 1;
rigidbody.drag = 0;
rigidbody.useGravity = false;
}
if (Input.GetMouseButtonUp (1) )
{
anim.SetBool ("isTransform", true);
anim.SetBool ("isIdle", true);
rigidbody.AddForce (Vector3.down * jumpSpeed);
rigidbody.AddForce (Vector3.forward * speed);
rigidbody.mass = 4;
rigidbody.drag = 0;
rigidbody.useGravity = true;
Debug.Log ("We are going down");
}
////////MouseInput//////////////////////////////////////////
////////////////Joystick Input//////////////////////////////////
if(Input.GetKeyDown(KeyCode.JoystickButton0))
{
anim.SetBool ("isTransform", true);
anim.SetBool ("isIdle", false);
rigidbody.AddForce (Vector3.up * jumpSpeed);
rigidbody.mass = 1;
rigidbody.drag = 0;
rigidbody.useGravity = false;
Debug.Log("I am pressing button0 on controller and going up");
}
if (Input.GetKeyUp (KeyCode.JoystickButton0))
{
anim.SetBool ("isTransform", true);
anim.SetBool ("isIdle", true);
rigidbody.AddForce (Vector3.down * jumpSpeed);
rigidbody.mass = 4;
rigidbody.drag = 0;
rigidbody.useGravity = true;
Debug.Log("I am pressing button0 on controller, and going down");
}
/////////////////////////////////////////////////////////////////////////////////////////
}
}
Your answer
Follow this Question
Related Questions
Google Play Games Services process reward data? C# 0 Answers
Scriptable object sub-classes list 1 Answer
2d game question boxes 0 Answers