Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Hertex · Jun 29, 2019 at 01:12 PM · movement3rd person controllerclimbingclimb

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;
         }
     }
 }
 










Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges