- Home /
Question by
Harears232 · May 23, 2017 at 06:22 AM ·
glitch
Player Controller and Terrain C#
Player somehow can just jump up the terrain walls by pressing space. (They don't fall back down.) The capsule just sticks to whatever it jumps onto. Any fixes!
using System.Collections; using UnityEngine; using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float walkSpeed = 1.0F;
public float jumpSpeed = 2.0F;
public float runSpeed = 5.0F;
public float gravity = 300.0F;
public Text countText;
public Text winText;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
private int count;
private Rigidbody rb;
private float canJump = 0f;
private bool isJumping;
private void jump(){
if(!isJumping){
isJumping = true;
Invoke("resetIsJumping", 1.5f);
}
}
void Start()
{
controller = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
Cursor.lockState = CursorLockMode.Locked;
}
private void resetIsJumping(){
isJumping = false;
}
void Update()
{
if (controller.isGrounded) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton ("Jump"))
moveDirection.y = jumpSpeed;
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Rocks Collected: " + count.ToString ();
if (count >= 30)
{
winText.text = "You Win!";
}
}
}
Comment
Hey, the box collider fixes the problem where the player could just walk up the walls. But there's another problem. The player can also just spam-jump up the walls, and somehow stick to them without falling back down. (i have locked x, y, z). Do you know what else i could add?
Disable the player movement's input until they collide with the terrain again.