Need Help with my climbing system
The climbing system: First of all i want to explain the basics of my climbing system: This system is for ladder like climbing. For this i need two objects: the player object and the climbable object. the climbable object has three child objects: enterPoint(also used as bottomPoint), topPoint and exitPoint(Used to set player position once it reaches the topPoint). I use CharacerController.Move() to move the player. The TP_Player script interacts with ClimbableObject script to get the child gameObjects from before. Both player and climbable objects have rigidbody with isKinematic setting disabled and a trigger collider. So when the player collider hits the climbableObject collider the player can climb by pressing the action button.
The trouble: So the trouble comes when i try to make my player to be centered with the climbableObject, for this i use the conventional transform.position = some vector3 position and it does centers the player with the climbableObject but the player cannot move after beign centered. i tried to disable the character controller component when centering the player but it just wont work, The player seems to be moving but remains stuck on the same place, like if is colliding with something. The position rises a bit and then goes back to previous value like there was something than wont let the player go up.
The TP_Player script: It holds every player physics info and methods.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TP_Player : MonoBehaviour
{
public enum PlayerState
{
Grounded,
Swimming,
Climbing,
};
public PlayerState state = PlayerState.Grounded;
public float moveSpeed = 4;
public float gravity = 9;
public float rotateSpeed = 500;
public float climbStep = 3;
public float climbSmooth = 0.2f;
float v = 0;
float h = 0;
bool isClimbing = false;
Vector3 moveVector = Vector3.zero;
Vector3 moveDirection = Vector3.zero;
Vector3 right = Vector3.zero;
Vector3 forward = Vector3.zero;
Vector3 vel = Vector3.zero;
public GameObject climbTarget;
public GameObject climbExit;
public GameObject climbEnter;
public GameObject climbTop;
CharacterController controller;
// Start is called before the first frame update
void Awake()
{
moveDirection = transform.TransformDirection(Vector3.forward);
controller = GetComponent<CharacterController>();
state = PlayerState.Grounded;
}
// Update is called once per frame
void Update()
{
//if(controller.isGrounded)
//state = PlayerState.Grounded;
switch(state)
{
case PlayerState.Grounded:
Grounded();
break;
case PlayerState.Swimming:
Swimming();
break;
case PlayerState.Climbing:
isClimbing = true;
//controller.enabled = false;
if (transform.position.y > climbTop.transform.position.y)
transform.position = climbTop.transform.position;
else
transform.position = climbEnter.transform.position;
Climbing();
break;
}
}
void Grounded()
{
v = Input.GetAxisRaw("Vertical");
h = Input.GetAxisRaw("Horizontal");
if (controller.isGrounded)
{
UpdateMovementDirection(h, v);
}
ApplyGravity();
controller.Move(moveVector * Time.deltaTime);
}
void Swimming()
{
}
void Climbing()
{
//Debug.Log("position: " + climbEnter.transform.position);
//Debug.Log("localPosition: " + climbEnter.transform.localPosition);
//Vector3 startVector = climbTarget.transform.position;
if (isClimbing)
{
if (Input.GetButtonDown("action"))
{
isClimbing = false;
controller.enabled = true;
}
UpdateClimbingMovement();
if(transform.position.y > climbTop.transform.position.y)
{
transform.position = climbExit.transform.position;
state = PlayerState.Grounded;
isClimbing = false;
controller.enabled = true;
}
}
}
void UpdateClimbingMovement()
{
v = Input.GetAxisRaw("Vertical");
moveVector = new Vector3(0, v * climbStep * climbSmooth, 0);
controller.Move(moveVector * Time.deltaTime);
//transform.position += new Vector3(0, v * climbStep * climbSmooth * Time.deltaTime, 0); ;
}
private void ApplyGravity()
{
moveVector.y -= gravity;
}
void UpdateMovementDirection(float h, float v)
{
Transform cameraTransform = Camera.main.transform;
forward = cameraTransform.forward;
forward.y = 0;
right = new Vector3(forward.z, 0, -forward.x);
Vector3 targetDirection = h * right + v * forward;
if(targetDirection != Vector3.zero)
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
}
transform.rotation = Quaternion.LookRotation(moveDirection);
if (v != 0 || h != 0)
moveVector = moveDirection * moveSpeed;
else
moveVector = Vector3.zero;
}
public bool isMoving()
{
if (moveVector != Vector3.zero)
return true;
else
return false;
}
public Vector3 GetPosition()
{
return transform.position;
}
}
The ClimbableObject script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClimbableObject : MonoBehaviour
{
GameObject player;
GameObject climbText;
bool isColiding = false;
bool isClimbing = false;
void Start()
{
player = GameObject.Find("Player");
climbText = GameObject.Find("txtClimb");
//Debug.Log("sdfalsdfdhskjashfdjskls");
}
private void Update()
{
if(isColiding)
{
if(isClimbing && player.GetComponent<TP_Player>().state == TP_Player.PlayerState.Climbing)
{
if (Input.GetButtonDown("action"))
{
player.GetComponent<TP_Player>().state = TP_Player.PlayerState.Grounded;
isClimbing = false;
}
}
else
{
if (Input.GetButtonDown("action"))
{
player.GetComponent<TP_Player>().state = TP_Player.PlayerState.Climbing;
}
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
isColiding = true;
climbText.GetComponent<Text>().text = "Climb";
player.GetComponent<TP_Player>().climbTarget = this.gameObject;
player.GetComponent<TP_Player>().climbExit = this.transform.Find("exitPoint").gameObject;
player.GetComponent<TP_Player>().climbEnter = this.transform.Find("enterPoint").gameObject;
player.GetComponent<TP_Player>().climbTop = this.transform.Find("topPoint").gameObject;
if (Input.GetButtonDown("action"))
{
player.GetComponent<TP_Player>().state = TP_Player.PlayerState.Climbing;
isClimbing = true;
}
}
}
private void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
isColiding = false;
climbText.GetComponent<Text>().text = "action";
player.GetComponent<TP_Player>().climbTarget = null;
player.GetComponent<TP_Player>().climbExit = null;
player.GetComponent<TP_Player>().climbTop = null;
player.GetComponent<TP_Player>().climbEnter = null;
player.GetComponent<TP_Player>().state = TP_Player.PlayerState.Grounded;
}
}
}
Your answer
Follow this Question
Related Questions
Move Player With Moving Vehicle 1 Answer
Character only falls when button is pushed down 0 Answers
3rd person movement with camera between platforms with gravity 0 Answers
3D character sliding movement,3D Character Sliding 1 Answer
Cant find why character doesnt move forward with this movement script 0 Answers