- Home /
Character Controller problem with enum
Hello, i am having a problem and i just cannot figure out what is wrong or why it is not working for me. so i have a character controller script with jump, walk, run and gravity is applied and it depends on enum to change states between walk jump run crouch fall animations, it is all working except the jump animation, if i try to access the other script directly it works but it keeps repeating itself. and the character jumps very low and increasing the jump speed gives a teleport like look then the gravity gets applied. what am i doing wrong ?
public class MarCont : MonoBehaviour {
CharacterController CC ;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 9.8F;
private Vector3 moveDirection = Vector3.zero;
public float rotationSpeed = 100.0F;
float extSpeed = 1.0f;
public AnimationMar AM;
public enum Status {Idle, Walk, Run, Crouch, Jump, extraPwr, Fall,WalkBack};
public Status MarioStat = Status.Idle;
Status MarioLStat = Status.Idle;
bool isControlled =true;
void Awake() {
AM.Idle();
}
void Start() {
CC = GetComponent<CharacterController>();
}
void Update() {
if (isControlled) {
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
transform.Rotate(0, rotation, 0);
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")*extSpeed);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(CC.isGrounded){
moveDirection.y=0;
if (Input.GetKeyDown("space")){
moveDirection.y = jumpSpeed*extSpeed;
if(extSpeed>1){MarioStat = Status.extraPwr;}
else if(extSpeed<2){
MarioStat=Status.Jump;
}
}
}else{
if(moveDirection.y<0 && MarioStat!= Status.Jump && MarioStat != Status.extraPwr){
MarioStat= Status.Fall;
}}
}else{
if(CC.isGrounded){
moveDirection.y = 0.5f*speed*extSpeed;
MarioStat = Status.Walk;
}
}
moveDirection.y -= gravity * Time.deltaTime;
CC.Move(moveDirection * Time.deltaTime);
if(Input.GetKey (KeyCode.LeftShift)){
extSpeed = 1.6f;
}else{ extSpeed = 1.0f;
}
if(CC.isGrounded &&Input.GetKey("s")){
// MarioStat = Status.WalkBack;
AM.WalkBack();
}
if(moveDirection.z!=0){
if(extSpeed>1){MarioStat = Status.Run;}
else{ MarioStat = Status.Walk;}
}else{
if(extSpeed>1){MarioStat = Status.Crouch;}
else{ MarioStat = Status.Idle;}
}
if(MarioStat != MarioLStat){
switch (MarioStat){
case Status.Idle:
AM.Idle();
break;
case Status.Walk:
AM.Walk ();
break;
case Status.Run:
AM.Run();
break;
case Status.Crouch:
AM.Crouch();
break;
case Status.Jump:
AM.Jump ();
break;
case Status.extraPwr:
AM.extJump();
break;
case Status.Fall:
AM.Fall ();
break;
case Status.WalkBack:
AM.WalkBack();
break;
}
}
moveDirection.y -= gravity*Time.deltaTime;
CC.Move(moveDirection*Time.deltaTime);
MarioLStat = MarioStat;
}
}
and here is my animation script.
public class AnimationMar : MonoBehaviour {
public void Idle (){
animation.CrossFade("idle");
}
public void Run () {
animation.CrossFade("sprint");
}
public void Walk (){
animation["walk"].speed = 1 ;
animation.CrossFade("walk");
}
public void WalkBack (){
animation["walk"].speed = -1;
animation.CrossFade("walk");
}
public void Crouch (){
animation.CrossFade("crouchIdle");
}
public void Jump (){
animation.Play("jump_1");
}
public void extJump (){
animation.CrossFade("jump_2");
}
public void Fall (){
animation.CrossFade("jumpFall");
}
}
any help or advice would be appreciated, thank you.
Your answer
Follow this Question
Related Questions
Character returning to start after run jump 1 Answer
begginer question (jump animation) 0 Answers
Increase jump height/distance 0 Answers
Switching between animations 1 Answer