Created
January 14, 2021 14:57
-
-
Save TheCuttlefish/0adc83db495776ad7ef209331dd2a5bc to your computer and use it in GitHub Desktop.
Player Movement + Climbing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Player : MonoBehaviour | |
{ | |
float speedX = 10f; | |
bool isClimbing = false; | |
float inputX; | |
float inputY; | |
void Update() | |
{ | |
Movement(); | |
Climbing(); | |
} | |
void Movement() | |
{ | |
inputX = Input.GetAxis("Horizontal"); | |
inputY = Input.GetAxis("Vertical"); | |
GetComponent<Rigidbody2D>().velocity = new Vector2(inputX * speedX, GetComponent<Rigidbody2D>().velocity.y); | |
} | |
void Climbing() | |
{ | |
if (isClimbing) | |
{ | |
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, inputY * speedX); | |
} | |
} | |
private void OnCollisionEnter2D(Collision2D collision) | |
{ | |
if(collision.transform.tag == "Wall") | |
{ | |
isClimbing = true; | |
GetComponent<SpriteRenderer>().color = Color.red; | |
} | |
} | |
private void OnCollisionExit2D(Collision2D collision) | |
{ | |
if (collision.transform.tag == "Wall") | |
{ | |
isClimbing = false; | |
GetComponent<SpriteRenderer>().color = Color.blue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment