I coded this script in C#
I've pasted below the code for my entire player controller. Jumping and double jumping works fine but whenever i wall jump it just go's up the wall but doesn't push away from it. I am trying to get wall jumping similar to supermeatboy.
I thought it had something to do with the "moveVelocity = 0f;" which would just stop the player in mid air but i don't think that is the only problem because i put that in a "If (WallJumped == false)" so that if you did wall jump then it shouldn't stop your velocity. Any help would be greatly appreciated thanks.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private float moveVelocity;
public float jumpHeight;
public float awayHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
public Transform wallCheck;
public float wallCheckRadius;
public LayerMask whatIsWall;
private bool onWall;
private bool doubleJumped;
private Animator anim;
private PlayerController player;
//public GameObject respawnParticle;
private Rigidbody2D rigid;
private bool wallJumped;
// Use this for initialization
void Start()
{
anim = GetComponent();
player = FindObjectOfType();
player.GetComponent().enabled = true;
//Instantiate (respawnParticle, player.transform.position, player.transform.rotation);
rigid = GetComponent();
wallJumped = false;
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
onWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W) && grounded)
{
Jump();
}
if (Input.GetKeyDown(KeyCode.W) && !doubleJumped && !grounded)
{
Jump();
doubleJumped = true;
}
if (grounded)
{
doubleJumped = false;
}
anim.SetBool("Grounded", grounded);
if (onWall)
{
if (wallJumped == false)
{
doubleJumped = false;
}
else
{
doubleJumped = true;
}
if (Input.GetKeyDown(KeyCode.W) && doubleJumped == false)
{
JumpAwayFromWall();
wallJumped = true;
}
if (rigid.velocity.y < 0)
{
GetComponent().gravityScale = 2f;
}
else
{
GetComponent().gravityScale = 3.5f;
}
}
else
{
GetComponent().gravityScale = 3.5f;
wallJumped = false;
}
anim.SetBool("OnWall", onWall);
moveVelocity = 0f;
anim.SetFloat("Speed", Mathf.Abs(GetComponent().velocity.x));
if (GetComponent().velocity.x > 0)
transform.localScale = new Vector3(0.25f, 0.25f, 1f);
else if (GetComponent().velocity.x < 0)
transform.localScale = new Vector3(-0.25f, 0.25f, 1f);
if (Input.GetKey(KeyCode.D))
{
//GetComponent().velocity = new Vector2(moveSpeed, GetComponent().velocity.y);
moveVelocity = moveSpeed;
}
if (Input.GetKey(KeyCode.A))
{
//GetComponent().velocity = new Vector2(-moveSpeed, GetComponent().velocity.y);
moveVelocity = -moveSpeed;
}
GetComponent().velocity = new Vector2(moveVelocity, GetComponent().velocity.y);
}
public void Jump()
{
GetComponent().velocity = new Vector2(GetComponent().velocity.x, jumpHeight);
}
public void JumpAwayFromWall()
{
GetComponent().AddForce(new Vector2(awayHeight, jumpHeight));
//GetComponent().velocity = new Vector2(awayHeight, jumpHeight);
//GetComponent().velocity = new Vector2(GetComponent().velocity.y, jumpHeight);
//GetComponent().velocity = new Vector2(GetComponent().velocity.x, jumpHeight);
//GetComponent ().velocity = new Vector2(-knockback, knockback);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.transform.tag == "MovingPlatform")
{
transform.parent = other.transform;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.transform.tag == "MovingPlatform")
{
transform.parent = null;
}
}
private void Flip()
{
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
↧