So I’m trying to make a simple script that makes it so that the player moves upward when you hold down Space or the LB button. For some reason it works as intended the first time but afterward the player just sort of moves slightly upward instead of progressing higher and higher. I’m not sure why this isn’t working, since technically it does work the first time but then not later. Here is the code for my movement controller. The parts I’m using for vertical movement are highlighted:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
public CharacterController controller;
public float speed = 4f;
public float gravity = -9f;
private GameObject ClimbController;
public bool CanClimb = false;
public bool IsLiquid = true;
Vector3 velocity;
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
float playerclimb = 0.2f;
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetButton("Climb"))
{
playerclimb = playerclimb + 0.1f;
transform.position += transform.up * playerclimb;
}
if (Input.GetButtonUp("Climb"))
{
playerclimb = 0.2f;
}
}
}
In your inspector window in the top right, click the … button and change from “Normal” to “Debug”. Now play your game and watch the value of playerclimb in the inspector. When happens when you try to climb?
I do notice that your velocity is being constantly added to by gravity but doesn’t appear to ever be reset to 0. Is it possible that your ever-increasing velocity is just canceling out your ever-growing playerclimb?
playerclimb isn’t “ever-growing”, it’s reset to 0.2 by the first bolded line every time Update runs.
Actually, “reset” isn’t the right word, since it’s a local variable. It’s newly created with a value of 0.2 every time Update runs and then forgotten at the end of Update.
Yea basically the idea is that once you let go of space/LB the process can repeat itself again the next time you hold time space/LB. Except that doesn’t happen for some reason. The next time around the player just sort of briefly moves up a bit and then nothing happens.
I uploaded a gif to show the problem. Actually it would appear that sometimes it DOES work the second time but for some reason only partially. Then it just stops working at all.
The if on lines 46-52 does absolutely nothing, since it only modifies a local variable that isn’t used after that point.
But the actual climbing code is effectively a simple “move the player up by 0.3 for every frame that this button is held down”. There’s very little that could go wrong in lines 36-44, so it’s probably not that this code is behaving differently but that something else is moving the player with enough force that it overrides the climbing.
I’d guess that @StarManta is correct that the problem has to do with the fact that you are making “velocity” bigger and bigger in the direction of gravity and never reset it. So it’s probably not “works once” but “works until a certain amount of time has passed”, at which point the player is “falling” so quickly that the movement from climbing isn’t fast enough to win.
What I see in the gif seems consistent with the idea that velocity is just getting bigger and bigger. I see a few attempts to “climb” in the gif. First your rise up, and then slowly fall to the floor. Then you rise up, but fall faster. Then you’re unable to rise at all.
So yeah, you need to figure out when to reset velocity to 0 (on collision with the ground, maybe). Or, just let Unity’s physics handle gravity?