hi community thanks for the help, im making a 2d game where the player can get in the walls and go up and down(ninja gaiden nes style), the X axis movement is not allowed, it only does it when is on the ground, so when the character is on the wall and the player presses the “Jump” button the character jumps to the opposite direction. For example if he is facing right and jumps then he goes left and the animation flips to, so the problem is that when the character touches the ground the X axis movement is wrong with the animation, for example if i go right the collider and everything goes right indeed but the animation is “running” like im pressing the left button. what i want is that when the character touches the ground the X axis stays correctly with the animation. any idea?? thanks!!!
This is my code so far:
using UnityEngine;
using System.Collections;
public class fos_controller_script : MonoBehaviour {
public float maxSpeed = 6;
public float normalSpeed = 4;
public float noSpeed = 0;
bool facingRight = true;
Animator anim;
Rigidbody2D rBody2D;
GameObject character;
bool grounded = false;
bool touchingWall = false;
public Transform groundCheck;
public Transform wallCheck;
float groundRadius = 0.05f;
float wallRadius = 0.05f;
float edgeCheckUpRadius = 0.05f;
public LayerMask whatIsGround;
public LayerMask whatIsWall;
public LayerMask whatIsEdgeUp;
public bool jump;
public bool superSpeed = false ;
public bool isMovingUp = false;
public bool isMovingDown = false;
public float jumpForce = 450f;
///wallJump Facing Right
public float negativeWallJumpForceX = -700f;
///wallJump Facing left
public float WallJumpForceX = 700f;
void Start () {
anim = GetComponent<Animator>();
rBody2D = gameObject.GetComponent<Rigidbody2D>();
}
void SetGravity(){
rBody2D.gravityScale = 0;
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
touchingWall = Physics2D.OverlapCircle(wallCheck.position, wallRadius, whatIsWall);
anim.SetBool("Ground", grounded);
anim.SetBool("touchingWall", touchingWall);
if(grounded && !touchingWall){
anim.SetBool ("wallJumpRight", false);
anim.SetBool ("wallJumpLeft", false);
//normalSpeed
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * normalSpeed, GetComponent<Rigidbody2D>().velocity.y);
superSpeed = false;
anim.SetBool ("superSpeed", superSpeed);
///// Facing right or left
if(move > 0 && !facingRight){
Flip ();
}
else if(move < 0 && facingRight){
Flip ();
}
//superSpeed / Joystick Button 15
if (Input.GetButton ("Fire1") && Input.GetButton ("Horizontal")) {
float moveFaster = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (moveFaster));
GetComponent<Rigidbody2D>().velocity = new Vector2 (moveFaster * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
superSpeed = true;
anim.SetBool("superSpeed", superSpeed);
}
if (superSpeed) {
if (Input.GetButton("Jump")) {
superSpeed = true;
anim.SetBool ("superSpeed", superSpeed);
}
}
}
//// Walltouching Code
if (touchingWall) {
superSpeed =false;
grounded =false;
anim.SetBool ("superSpeed", false);
anim.SetBool ("Ground", false);
rBody2D.gravityScale = 0;
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
anim.SetBool ("touchingWall", true);
if (Input.GetAxis ("Vertical") > 0) {
rBody2D.gravityScale = 0;
transform.Translate (0, 2 * Time.deltaTime, 0);
anim.SetBool ("isMovingUp", true);
}else {
anim.SetBool ("isMovingUp", false);
}
if (Input.GetAxis ("Vertical") < 0) {
rBody2D.gravityScale = 0;
transform.Translate (0, -2 * Time.deltaTime, 0);
anim.SetBool ("isMovingDown", true);
} else {
anim.SetBool ("isMovingDown", false);
}
if (Input.GetButton ("Jump") && facingRight ) {
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
GetComponent<Rigidbody2D>().AddForce (new Vector2 (negativeWallJumpForceX, 450));
GetComponent<Rigidbody2D>().velocity = new Vector2 (0, 0);
rBody2D.gravityScale = 1;
anim.SetBool ("isMovingUp", false);
anim.SetBool ("isMovingDown", false);
}
if (Input.GetButton ("Jump") && !facingRight ) {
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
GetComponent<Rigidbody2D>().AddForce (new Vector2 (WallJumpForceX, 450));
GetComponent<Rigidbody2D>().velocity = new Vector2 (0, 0);
rBody2D.gravityScale = 1;
anim.SetBool ("isMovingUp", false);
anim.SetBool ("isMovingDown", false);
}
}
}
void Update()
{
////Jump Button
if (grounded && Input.GetButton ("Jump") ) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, 0);
GetComponent<Rigidbody2D>().AddForce (new Vector2 (0, jumpForce));
anim.SetBool("Ground", false);
}
//SuperSpeed
if (Input.GetButton ("Fire1") && Input.GetButton ("Horizontal")) {
float moveFaster = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (moveFaster));
GetComponent<Rigidbody2D>().velocity = new Vector2 (moveFaster * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
superSpeed = true;
anim.SetBool("superSpeed", superSpeed);
}
}
void Flip(){
if (grounded) {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}