Unity Platform 06 Ledge Climb
===
2020.08.13
###### tags: `platform`
## List
1. [Ledge Climb](#LC)
2. [Script](#S)
3. [Editor](#E)
## Content
### Ledge Climb<a id="LC"/>
Climbing ledge is one of charactor's movement.<br>Consider it with animation and position.<br>animtion is only show images set, so, must set postion in script for smoothing with animation.<br><br>
### Script<a id="S"/>
Make it with some variable.<br><br>First, set conditions for climb action.<br>This conditions are applied like wall check
```csharp=
public Transform ledgeCheck;
private bool isTouchingLedge;
private bool canClimbLedge = false;
private bool ledgeDetected;
private void CheckSurroundings()
{
~~~
if (isTouchingWall && !isTouchingLedge && !ledgeDetected)
{
ledgeDetected = true;
ledgePosBot = wallCheck.position;
}
}
```
Second, check canClimb state, and set position after or ~ing climb action.<br>`ledgePos1` is first postion when start climb, `ledgePos2` is second postion when end climb<br>Set in `update()`
```csharp=
private Vector2 ledgePosBot;
private Vector2 ledgePos1;
private Vector2 ledgePos2;
public float ledgeClimbXOffset1 = 0f;
public float ledgeClimbYOffset1 = 0f;
public float ledgeClimbXOffset2 = 0f;
public float ledgeClimbYOffset2 = 0f;
private void CheckLedge()
{
if (ledgeDetected && !canClimbLedge)
{
canClimbLedge = true;
if (isFacingRight)
{
ledgePos1 = new Vector2(Mathf.Floor(ledgePosBot.x + wallCheckDistance) - ledgeClimbXOffset1,
Mathf.Floor(ledgePosBot.y) + ledgeClimbYOffset1);
ledgePos2 = new Vector2(Mathf.Floor(ledgePosBot.x + wallCheckDistance) + ledgeClimbXOffset2,
Mathf.Floor(ledgePosBot.y) + ledgeClimbYOffset2);
}
else
{
ledgePos1 = new Vector2(Mathf.Floor(ledgePosBot.x - wallCheckDistance) + ledgeClimbXOffset1,
Mathf.Floor(ledgePosBot.y) + ledgeClimbYOffset1);
ledgePos2 = new Vector2(Mathf.Floor(ledgePosBot.x - wallCheckDistance) - ledgeClimbXOffset2,
Mathf.Floor(ledgePosBot.y) + ledgeClimbYOffset2);
}
canMove = false;
canMove = true;
}
if (canClimbLedge)
{
transform.position = ledgePos1;
}
anim.SetBool("canClimbLedge", canClimbLedge);
}
```
Last, end animation, set position to `ledgePos2` and reset all conditions.<br>
```csharp=
public void FinishLedgeClimb()
{
canClimbLedge = false;
transform.position = ledgePos2;
canMove = true;
canFlip = true;
ledgeDetected = false;
anim.SetBool("canClimbLedge", canClimbLedge);
}
```
<br><br>
### Editor<a id="S"/>
Set animation controller.<br>Make Two transition and edit condition(*canClimbEdge*)<br>And Add `FinishClimb` Event in animtion window(not animation controller).<br>Setting offset needs to be changed with play window.