Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Last active January 18, 2019 17:35
Show Gist options
  • Save TheCuttlefish/cdaf7063601e8514b3f66f21f8807320 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/cdaf7063601e8514b3f66f21f8807320 to your computer and use it in GitHub Desktop.
Skateboarding Mechanic in 2D (align to ramp normals)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AllightToNormal : MonoBehaviour
{
public float speed;
void OnCollisionStay2D(Collision2D col)
{
Debug.DrawLine(transform.position, col.contacts[0].point, Color.green);
var newNormal = new Vector2(transform.position.x, transform.position.y) - col.contacts[0].point;
transform.up = Vector2.Lerp(transform.up, newNormal, 20f * Time.deltaTime);
// transform.up = col.transform.up;
//regular vector up rotation on collision
}
private void Update()
{
//basic input
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2 (Input.GetAxis("Horizontal") * speed, rb.velocity.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment