- Home /
Wall Climbing Help
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);
}
}
Answer by GeoMaru · Nov 18, 2013 at 10:31 AM
Maybe you should take off the gravity while you climb the wall, so even if you let go of the horizontal direction key, your character will still on the wall, it not exactly what you want, but this is what I can come up with for now.
Answer by HappyMoo · Jan 21, 2014 at 09:22 AM
Don't make your Climbing directly dependent on Collision. Manage that state yourself. How do you enter the Climbing state? How do you leave it? How does the controls change while you climb? How does the movement change while you climb?
Think about what makes sense, don't let the physics systems moods decide if you climb or not, define it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity can't find Character Motor and max forwad speed -1 Answers