- Home /
Climbing Not Working as Intended
I'm a newbie making a game where I want my character to climb all colliders such as box, mesh, etc... After watching about 20 tutorials, I have a bit of code that sort've works. My main character (a cylinder) can jump upwards and stick to cubes, a bridge I made, etc... and move sideways, however not upwards. Here's my code:
I'm learning lots but could really use some help as to why I can't move my first person cylinder upwards. Thanks for reading this!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
GameObject playerOBJ;
bool canClimb = false;
float speed = 1;
void OnCollisionEnter(Collision coll)
{
if (coll.gameObject.tag == "Player")
{
canClimb = true;
playerOBJ = coll.gameObject;
}
}
void OnCollisionExit(Collision coll2)
{
if (coll2.gameObject.tag == "Player")
{
canClimb = false;
playerOBJ = null;
}
}
void Update()
{
if (canClimb)
{
if (Input.GetKey(KeyCode.W))
{
playerOBJ.transform.Translate(new Vector3(0, 1, 0) * Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.S))
{
playerOBJ.transform.Translate(new Vector3(0, -1, 0) * Time.deltaTime * speed);
}
}
}
}
Your answer
Follow this Question
Related Questions
Climbing using OnTriggerEnter and Arrow Keys? 1 Answer
Rigidbody goes crazy when isKinematic is false 1 Answer
Ball passing through triggered collider 0 Answers
Help! How do we enable or disable Collider with a specific action? 2 Answers
Sprites not recognized as whole-spinning in different directions 0 Answers