Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
Soundmartell's avatar
Soundmartell
Explorer
9 years ago

How to make the OVRPlayerController Climb a Ladder?

Hello,
Is it any way to make the OVRPlayerController to Climb a Ladder?
I have some script to do that with the Standard First Person Controller but as the OVRPlayerController has not the First Person controller it doesn't work.
I will appreciate any help or support.
Best regards,

8 Replies

Replies have been turned off for this discussion
  • This belongs in the Developer section. Moving the thread. Thanks.
  • I'd make a trigger volume around the ladder, and when you enter it (or onInside) switch to a ladder control system in your player

    Without remembering the proper code:

    On the ladder with collider.Test the collider for a player entering
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    onTriggerEnter  (Collider thePlayerCollider){

    MyCustomController thePlayer =(
    MyCustomController) getComponent (ofType(
    MyCustomController));

    thePlayer.isClimbing = true;

    }




    inMyCustomControlle (ie you custom code inheritted from OVR controller

    update(){

    if (isClimbing)
    {
    doClimbMovement();
    }
    else
    {
    doUsualMovement();
    }
    }


    You'll need to add the actual climb anim and logic

    Does that make sense.

    Its prob what I'd do..but Im sure other people have better ideas.

    -P



  • pjenness said:


    I'd make a trigger volume around the ladder, and when you enter it (or onInside) switch to a ladder control system in your player

    Without remembering the proper code:

    On the ladder with collider.Test the collider for a player entering
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    onTriggerEnter  (Collider thePlayerCollider){

    MyCustomController thePlayer =(
    MyCustomController) getComponent (ofType(
    MyCustomController));

    thePlayer.isClimbing = true;

    }




    inMyCustomControlle (ie you custom code inheritted from OVR controller

    update(){

    if (isClimbing)
    {
    doClimbMovement();
    }
    else
    {
    doUsualMovement();
    }
    }


    You'll need to add the actual climb anim and logic

    Does that make sense.

    Its prob what I'd do..but Im sure other people have better ideas.

    -P






    Thank you. I am new in coding. I will try to figure out how to code a custom controller. 
    I will appreciate any sample script. I am sorry for being asking for to much.
    Regards.
  • Well, I created 2 new scripts and added a new function the the OVRPlayerController: 


    Script 1 OVRLadderController.cs  attached to the OVRPlayerController.





    using UnityEngine;
    using System.Collections;
    using UnityStandardAssets.CrossPlatformInput;
    using UnityStandardAssets.Utility;
        public class OVRLadderController : MonoBehaviour {
            public float speed = 5.0f;
            
           
            
            private CharacterController controller;
            private Vector3 moveDirection = Vector3.zero;
            
            public void _init(){
            }
            void Start () {
            
                controller = gameObject.GetComponent<CharacterController>();
            }
            void Update () {
                
                
                moveDirection = new Vector3(0, Input.GetAxis("Vertical"), 0);
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;
                controller.Move(moveDirection * Time.deltaTime);
            }        
        }
    Script 2 OVRLadder.cs Attached to the Ladder Collider.






    using UnityEngine;

    using System.Collections;




    public class OVRLadder : MonoBehaviour {


        


        private OVRPlayerController _fps = null;


        private OVRLadderController _lc = null;


        private bool _activated = false;


        private bool _use = false;


        private bool _has_controllers = false;




        void Start(){


            GameObject _go = GameObject.FindGameObjectWithTag("Player");


            if (_go != null) {


                _fps = _go.GetComponent<OVRPlayerController> ();


                _lc = _go.GetComponent<OVRLadderController> ();


                if ((_fps == null) || (_lc == null)) {


                    Debug.LogWarning ("Not found \"LadderController\" or / and \"OVRController\" components");


                } else {


                    _activated = true;


                }


            }


        }




        void Update() {


            if(_activated && _has_controllers && Input.GetKeyDown(KeyCode.F) || OVRInput.Get(OVRInput.Button.PrimaryThumbstick)){


                if(_use){


                    _use = _lc.enabled = false;


                    _fps.enabled = true;


                    _fps._init();


                } else {


                    _fps.enabled = false;


                    _use = _lc.enabled = true;


                    _lc._init();


                }


            }


        }



        void OnTriggerEnter () {


            if (GetComponent<Collider>().tag == "Ladder") {


                _has_controllers = true;


            }


        }


        void OnTriggerExit () {


            if (GetComponent<Collider> ().tag == "Ladder") {


                _has_controllers = false;


                if (_activated) {


                    _use = _lc.enabled = false;


                    _fps.enabled = true;


                    _fps._init ();


                    ///We Jump out of the Ladder.


                    _fps._JumpOutLadder ();




                }


            }


        }


    }
      


    New function in the OVRPlayerController:





    public void _JumpOutLadder(){
          MoveThrottle += new Vector3(0, 0, transform.lossyScale.z * LadderOutForce);

        }
      
    Now the FPS is Climbing Up without any problems but It can't climb down because the FPS keep moving in the Up direction and off course is exiting the trigger.
    Is it any way to detect if we are climbing Up or Down and change the FPS moving direction? Maybe using the Camera View Direction or Colliders.
    I will appreciate any help.
    Regards.


  • maybe have 2 more trigger volumes, one at the top and one at the bottom.

    As you enter the main ladder volume, if it also detects one of those volumes it sets the direction to climb up or down?
    These only get checked on entry to the ladder, as it you are alrady on the ladder you dont want them to activate.

    Also if you jump and cathc the ladder halfway you want the choice to go up or down to be yours.


    Jsut an idea

    -P
  • Hi Soundmartell
    I used you scripts to climb a ladder however I get the errors in the screenshots
    Can you please help me solving the problem.

    Regards
    Luis
  • Hi Soundmartell

    Im new to unit and I have used your scripts to climb a ladder, however I get the bellow errors.
    Can you please help me solving the problem.

    Regards
    Luis