2D Platformer, Icy material?
Hi,
Bare with me, I am still completely new to Unity. I have a simple 2D platformer that is coming along decently and I want the final level to be set in a Ice World. The premise would be that the player would find it quite difficult to control the player because after they stop moving, the character would still move because of the slipperiness of the ice.
My issue is that I do not know how to implement this. After doing a bit of research, I couldn't find anything that I could personally comprehend since it's all to do with physics. I know it isn't as simple as making a 2D physics material. Here is my player movement script which is a mash up of my own code and other code across the web, apologies if it's a mess :)
public class PlayerMovement : MonoBehaviour {
public int speed = 2;
[Range(1, 10)]
public float jumpVelocity = 5f;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
public float groundedSkin = 0.5f;
public LayerMask mask;
public AudioClip jump;
private AudioSource audioSource;
private SpriteRenderer mySpriteRenderer;
Animator anim;
bool jumpRequest;
bool grounded;
Vector2 playerSize;
Vector2 boxSize;
Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
playerSize = GetComponent<BoxCollider2D>().bounds.size;
boxSize = new Vector2(playerSize.x, groundedSkin);
mySpriteRenderer = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis ("Horizontal")));
if (Input.GetAxisRaw("Horizontal") > 0)
{
transform.Translate(speed * Time.deltaTime, 0f, 0f);
//rb.velocity = new Vector2(speed * Time.deltaTime, 0f);
mySpriteRenderer.flipX = false;
}
if (mySpriteRenderer != null)
{
if (Input.GetAxisRaw("Horizontal") < 0)
{
transform.Translate(-speed * Time.deltaTime, 0f, 0f);
//rb.velocity = new Vector2(speed * Time.deltaTime, 0f);
mySpriteRenderer.flipX = true;
}
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
else if ((rb.velocity.y > 0 && !Input.GetKey(KeyCode.UpArrow)))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
}
if ((Input.GetKeyDown(KeyCode.UpArrow) && grounded))
{
jumpRequest = true;
AudioSource.PlayClipAtPoint(jump, this.transform.position);
}
if (jumpRequest)
{
rb.AddForce(Vector2.up * jumpVelocity, ForceMode2D.Impulse);
jumpRequest = false;
grounded = false;
}
else
{
Vector2 boxCenter = (Vector2)transform.position + Vector2.down * (playerSize.y + boxSize.y) * 0.5f;
grounded = (Physics2D.OverlapBox (boxCenter, boxSize, 0f, mask) != null);
}
}
}
To round it up, if anyone could help me understand where to implement the icy movement code and specifically how to do so, would be greatly appreciated. If you need any other resources from the game, feel free to ask.
Thanks!