Hello,
I am having some major issues trying to work with the new 2D implementation in Unity. I know, it’s brand new. In fact, it’s so brand new that trying to look up for answers regarding most things 2D is a pain (to say the least). After days of aggrivation I have no choice but to try to see if you guys could help.
I am TRYING to get the 2D Raycasting to work properly in my 2D game. You see, I want to: use raycasting to draw a line in front of the player (and show it in Debug), if the raycast hits a wall while the player is jumping, the wallSlide variable is true, if the wallSlide variable is true, animation.SetBool(“wallSlide”, true) to play the wall slide animation, then put it into my Jump function so that I could use it to wall jump.
Here’s what the code looks like:
private float maxSpeed = 50.0f; //the max speed allowed for the character
bool facingRight = true; //is the character facing right?
//Creates an animatior component
private Animator anim;
//All of these are for us to check if the player is grounded
bool grounded = false;
public Transform groundCheck; //creates a groundCheck variable @ the player's location
float groundRadius = 0.2f; //creates the radius of the circle collider
public LayerMask whatIsGrounded; //will check layers to find what is the ground
//Jump Force value, doubleJump, and wallSlide set up
private float jumpForce = 3250.0f;
bool doubleJump = false;
bool wallSlide = false;
bool wallHit = false;
// Use this for initialization
void Start () {
// Assigns the Animator to the anim variable
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
Movement();
}
void Update(){
Jumping();
Raycasting();
}
void Movement(){
if (grounded){
doubleJump = false;
wallSlide = false;
}
//Constant 2D check to see if the player is on the ground
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGrounded);
anim.SetBool("Ground", grounded);
//Sets up the vertical speed for the jumping up/down velocity
anim.SetFloat("vSpeed",rigidbody2D.velocity.y);
//Assigns for Horizontal movement, applied to a float value
float move = Input.GetAxis("Horizontal");
//Allows for smooth movement w/o need of Time.deltaTime
anim.SetFloat ("speed", Mathf.Abs(move));
//Creates an eaiser way to add velocity
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
//World Flip checks
//if facing right, Flip so character is facing right
if ((move > 0.0f) !facingRight){
Flip();
}
//if facing left, Flip so character is facing left
else if ((move < 0.0f) facingRight){
Flip();
}
}
void Jumping(){
//if grounded can use the Space key to jump
if ((grounded || !doubleJump || !wallSlide) Input.GetKeyDown(KeyCode.Space)){
//If grounded, jump with a new vector2 and y = jumpForce
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
//if not grounded, player is already in the air, and space is pressed, double jump
if(!doubleJump !grounded !wallSlide){
doubleJump = true;
}
}
}
void Flip(){
facingRight = !facingRight; //if facingRight =/= true
Vector3 theScale = transform.localScale; //Create a new Vector3 localScaling of the World
theScale.x *= -1; //flips the world so it's going opposite
transform.localScale = theScale; //make the reversed view the new localScale of the transform
}
void Raycasting(){
//use raycast to draw a line in front of the player
//if raycast hits a wall while jumping goto wallslide = true
//if wallSlide = true, ani.SetBool("wallSlide", wallSlide)
//call the function inside the
/*
RaycastHit2D hit;
Ray2D originPos = rigidbody2D.transform.right;
if(Physics2D.Raycast(hit, out originPos)){
wallHit = true;
}
if(wallHit){
anim.SetBool("WallSlide", true);
}*/
}
}
The issues I’m having are; a) how the f*!k do I implement a Ray 2D collider, and b) how the hell do I use it to check for a wallHit, then how do I make the wall hit allow for the animation “WallSlide” to occur? Everytime I try to implement this system, I end up confused on what to do, and most tutorials out there aren’t helping.
In the 3D variation, it seems a LOT easier. But I really, REALLY, wished they covered raycast for 2D in their tutorials/live training. There just isn’t enough help out there for people trying to do this kind of stuff. Thanks.