Skip to content

Commit

Permalink
Merge pull request #2 from yuzhengwen/main
Browse files Browse the repository at this point in the history
Merge changes to branch
  • Loading branch information
yuzhengwen committed Mar 11, 2024
2 parents da1bafe + 6cbe621 commit 7ab57c9
Show file tree
Hide file tree
Showing 5 changed files with 971 additions and 47 deletions.
113 changes: 113 additions & 0 deletions Assets/DynamicController.cs
@@ -0,0 +1,113 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DynamicController : MonoBehaviour
{
protected Rigidbody2D rb;
protected Collider2D col;
public LayerMask groundLayer;
public float xInput;
public float moveSpeed = 5f;
public float maxSlopeAngle = 45;

public float slopeCheckDistance = 0.5f;
private float slopeDownAngle;
private float slopeSideAngle;
private float lastSlopeAngle;

private bool onSlope;
private bool isGrounded;

Vector2 groundParallel;

void Start()
{
rb = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
}

// Update is called once per frame
void Update()
{
xInput = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
float targetXVel = xInput * moveSpeed;

Vector2 bottomCenter = new(col.bounds.center.x, col.bounds.min.y);
CheckGround();
SlopeCheckHorizontal(bottomCenter);
SlopeCheckVertical(bottomCenter);
SetVelocity(targetXVel);
}
private void SetVelocity(float targetXVel)
{
if (isGrounded && !onSlope) //if not on slope
{
rb.velocity = new(targetXVel, 0.0f);
}
else if (isGrounded && onSlope) //If on slope
{
rb.velocity = new(moveSpeed * groundParallel.x * -xInput, moveSpeed * groundParallel.y * -xInput);
}
else if (!isGrounded) //If in air
{
rb.velocity = new(targetXVel, rb.velocity.y);
}
}
private void CheckGround()
{
isGrounded = Physics2D.BoxCast(col.bounds.center, col.bounds.size, 0f, Vector2.down, 0.1f, groundLayer);
}

private void SlopeCheckHorizontal(Vector2 bottomCenter)
{
RaycastHit2D hitRight = Physics2D.Raycast(bottomCenter, transform.right, slopeCheckDistance, groundLayer);
RaycastHit2D hitLeft = Physics2D.Raycast(bottomCenter, -transform.right, slopeCheckDistance, groundLayer);

if (hitRight)
{
onSlope = true;
slopeSideAngle = Vector2.Angle(hitRight.normal, Vector2.up);
}
else if (hitLeft)
{
onSlope = true;
slopeSideAngle = Vector2.Angle(hitLeft.normal, Vector2.up);
}
else
{
slopeSideAngle = 0;
onSlope = false;
}
}
private void SlopeCheckVertical(Vector2 bottomCenter)
{
RaycastHit2D hit = Physics2D.Raycast(bottomCenter, Vector2.down, slopeCheckDistance, groundLayer);
if (hit)
{
groundParallel = Vector2.Perpendicular(hit.normal).normalized;
slopeDownAngle = Vector2.Angle(hit.normal, Vector2.up);

Debug.DrawRay(hit.point, hit.normal, Color.red);
Debug.DrawRay(transform.position, groundParallel, Color.red);

if (slopeDownAngle != lastSlopeAngle)
{
onSlope = true;
}
lastSlopeAngle = slopeDownAngle;
}
if (onSlope && xInput == 0)
{
rb.sharedMaterial.friction = 1;
}
else
{
rb.sharedMaterial.friction = 0;
}
}
}
11 changes: 11 additions & 0 deletions Assets/DynamicController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Assets/Player.physicsMaterial2D
@@ -0,0 +1,11 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!62 &6200000
PhysicsMaterial2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Player
friction: 0
bounciness: 0
8 changes: 8 additions & 0 deletions Assets/Player.physicsMaterial2D.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7ab57c9

Please sign in to comment.