Question by
anthonl00 · Oct 27, 2017 at 09:21 PM ·
collidercharactercontrollerontriggerenterontriggerexitontriggerstay
I'm trying to make a climbable ladder, but when I climb down, the character controller falls through the ground, Can anybody help me with this?
As mentioned above, once I go down on the ladder my character moves through the ground as if though the trigger on the box collider of the ground object is set to Is Trigger, even though the ladderTrigger script isn't attached to the ground object. It shouldn't be able to move through the ground, so my question is if there is any possible fix to this? here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LadderTrigger : MonoBehaviour {
public float speed = 5.0f;
public Transform target;
public PlayerMovement playerMovement;
public CharacterController controller;
void Start (){
controller = GetComponent<CharacterController> ();
playerMovement = GetComponent<PlayerMovement> ();
}
void OnTriggerStay (Collider other){
target = other.transform;
target.GetComponent<PlayerMovement> ().gravity = 0.0f;
target.GetComponent<CharacterController> ().slopeLimit = 0.0f;
target.GetComponent<CharacterController> ().stepOffset = 0.0f;
if (Input.GetKey ("w") || Input.GetKey (KeyCode.UpArrow)) {
target.Translate (Vector3.up * Time.deltaTime * speed, Space.World);
} else if (Input.GetKey ("s") || Input.GetKey (KeyCode.DownArrow)) {
target.Translate (Vector3.down * Time.deltaTime * speed, Space.World);
}
}
void OnTriggerExit (Collider other){
target = other.transform;
target.GetComponent<PlayerMovement> ().gravity = 20.0f;
target.GetComponent<CharacterController> ().slopeLimit = 45.0f;
target.GetComponent<CharacterController> ().stepOffset = 0.3f;
target = null;
}
}
Comment