Hi,
I am trying to make my character wall climb.. and it works, except that as soon as you let go of the horizontal direction key it will think its no longer colliding with the wall and fall down, I don't want the character to fall off until they either jump or reach the bottom. They shouldn't have to hold the key to stick to the wall. They should be able to stick to it.
Here is my code.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float MoveSpeed = 4f;
public float TurnSpeed = 10f;
public float JumpSpeed = 10f;
public float Gravity = 21f;
public float ClimbSpeed = 10f;
public CharacterController CharacterController;
private Vector3 moveDirection = Vector3.zero;
private float TerminalVelocity = 0f;
private float turnRotation = 0f;
private Quaternion desiredRotation;
private bool Climbing = false;
void Awake()
{
CharacterController = GetComponent("CharacterController") as CharacterController;
}
void Update()
{
float deadZone = 0.1f;
moveDirection = Vector3.zero;
//rotate character
if (Input.GetAxis("Horizontal") > deadZone)
turnRotation = 0f;
if (Input.GetAxis("Horizontal") < -deadZone)
turnRotation = 180f;
desiredRotation = Quaternion.Euler(0f, turnRotation, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, TurnSpeed * Time.deltaTime);
//move character
if(Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
moveDirection.z = Input.GetAxis("Horizontal");
if(moveDirection.magnitude > 1)
moveDirection.Normalize();
moveDirection *= MoveSpeed;
//jump
if(CharacterController.isGrounded && Climbing == false)
TerminalVelocity = 0f;
if(Input.GetButtonDown("Jump"))
{
TerminalVelocity = JumpSpeed;
}
//gravity
if((CharacterController.collisionFlags & CollisionFlags.Above) != 0)
{
if(TerminalVelocity > 0)
TerminalVelocity = -TerminalVelocity / 2;
}
TerminalVelocity -= Gravity * Time.deltaTime;
//wall climbing
if((CharacterController.collisionFlags & CollisionFlags.Sides) != 0)
{
TerminalVelocity = 0;
Climbing = true;
}
else
Climbing = false;
if((Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone) && Climbing == true)
{
moveDirection.y = Input.GetAxis("Vertical");
if(moveDirection.magnitude > 1)
moveDirection.Normalize();
moveDirection.y *= ClimbSpeed;
}
else
moveDirection.y = TerminalVelocity;
CharacterController.Move(moveDirection * Time.deltaTime);
}
}
↧