#Creating a ladder with the character controller

1 messages · Page 1 of 1 (latest)

brave ledge
#

I'm not using a rigid body controller and I'm having troubles making a ladder for the character controller

using System.Collections.Generic;
using UnityEngine;

public class Ladder : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    Vector3 velocity;
    public float thrust = 20f;

    public AudioSource sound;
    bool inside = false;
    
    void OnTriggerEnter(Collider other)
    {
         if (other.CompareTag("Player"))
         {
            Debug.Log("TouchingLadderTrue");
            inside = true;
         }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            inside = false;
        }
    }

    void Update()
    {
        if (inside == true && Input.GetKeyDown(KeyCode.W))
        {
            velocity.y = Mathf.Sqrt(thrust * speed * Time.deltaTime);
            
        }

        if (inside == true && Input.GetKeyDown(KeyCode.S))
        {
            //
            
        }

        if (inside == true && Input.GetKeyDown(KeyCode.W))
        {
            sound.enabled = true;
            sound.loop = true;
        }
        else
        {
            sound.enabled = false;
            sound.loop = false;
        }


    }

}```
hasty belfry
#

Where exactly is the problem? Is on the logic to make a ladder, the character controller functions...

brave ledge
#
using System.Collections.Generic;
using UnityEngine;

public class Ladder : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    Vector3 velocity;
    public float thrust = 20f;

    //public AudioSource sound;
    public bool inside = false;
    
    void OnTriggerEnter(Collider other)
    {
         if (other.CompareTag("Player"))
         {
            Debug.Log("TouchingLadderTrue");
            inside = true;
         }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            inside = false;
        }
    }

    void Update()
    {
        if (inside == true && Input.GetKeyDown(KeyCode.W))
        {
            //velocity.y = Mathf.Sqrt(thrust * speed * Time.deltaTime);
            controller.Move(transform.forward * speed * Time.deltaTime);
            controller.Move(transform.forward * thrust * Time.deltaTime);
            
        }

        if (inside == true && Input.GetKeyDown(KeyCode.S))
        {
            //
            
        }


    }

}```
#

I managed to make it kind of work

#

the problem is that it happens instantly

#

as long as I press W the player snaps on top of the ladder

#

I want it to be smoother

brave ledge
hasty belfry
#

Got it. I think you're right. Tbh with you, i haven't use too many times the CharacterController. So, why there are two controller.Move lines?

brave ledge
#

I just added the second line for testing purposes. It's basically the same thing.

hasty belfry
#

Got it. So the second line might be interfering on the move amount. If you remove it, will the player still snap on the ladder?

brave ledge
#

Yes, I just tried it

hasty belfry
#

What happens when you reduce the speed and press W on the ladder?

brave ledge
#

Like the gif shows

hasty belfry
#

Ok

#

I saw that you're using Transform.forward, this property is the axis Z of the object considering the rotation. But, as long as you have to increase the Y position when climb the ladder, i think you should use Tranform.up (which is the axis Y considering the rotation), instead of of .forward. Or did you used tranform.forward due to another reason?

brave ledge
#

I used Transform.up at first but the player was moving opposite to the ladder for some reason. With some trial and error I figured that only transform.forward makes the player go up.

#

The player goes up but it's not smooth at all currently

#

It happens in an instant

#

That's what I'm trying to resolve.

hasty belfry
#

With "move opposite", do you mean like move downwards instead of upwards?

hasty belfry
brave ledge
#

Pushes the player away from the ladder

hasty belfry
#

Got it

#

Atm, idk what is causing this problem. If i figure out something i'll let you know

brave ledge
#

Alright thanks 🙂

hasty belfry
#

You're welcome😄

#

If you have some question about how to implement this with the CharacterController.Move let me know

brave ledge
#

Yeah actually I have :p

hasty belfry
#

Is the question about something specific or something in general?

brave ledge
#

How do I use it with character.move?

hasty belfry
#

The Mathf.smoothDamp returns a float value, so you have to implement it on each direction (x,y and z) and convert it to a Vector3 value (the parameter used by characterController.Move()). But i readed a bit more, and i found the Vector3.smoothDamp, which seems to work like the Mathf function, but returns a Vector3 value. The docs of this function is https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

#

It takes 6 parameters. The first is the current object (player) position, the second is the target position, the third is a reference of the current velocity (so the method will change the velocity), the fourth is the smoothTime (the longer the smooth time is, the smoother the movement is), and two optionals parameters (an maxSpeed to clamp, and the deltaTime (which is Time.deltaTime as default))

#

If you have any question feel free to ask

brave ledge
#

I'm not sure about the implementation

#

I get the 6 parameters and what not I just don't know how to implement it on my code

#

Sorry but I'm not good with calculations and vectors 😅

hasty belfry