- Home /
Moving Character in Midair? JS
I feel like this question should be relatively simple, and I'm pretty sure I know the problem, but not quite how to fix it. I'm making a 2.5d platformer/fighting game, so I only need the character to move in two axes (x and y) as opposed to three. I've just started working on this, set up some code to have my hero run left, right, and jump, but I can't get my character to be able to move in midair.
#pragma strict
var moveDir : Vector3 = Vector3.zero;
var gravity : float;
private var theController : CharacterController;
var moveSpeed : float;
var jumpHeight : float;
function Start()
{
theController = GetComponent.<CharacterController>();
}
function Update()
{
if (theController.isGrounded == true)
{
moveDir = new Vector3(Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime, 0, 0);
if (Input.GetKeyDown(KeyCode.W))
{
moveDir.y = jumpHeight;
}
}
moveDir.y -= gravity * Time.deltaTime;
theController.Move(moveDir);
}
here's the preliminary code that does work, but doesn't allow movement when in midair. I tried moving the line: moveDir = new Vector3(...); outside of that if statement for isGrounded, but of course that solution was too easy to work right away. Turns out when that happens, it causes the character to teleport a fraction of the total jump and then come down. I think it has something to do with resetting the value of moveDir back to 0 in the y direction every frame if I move it outside. Anyway, if that's the issue though, I'm not sure then how to restructure it to get around that (assuming that's what's wrong at all). Any suggestions are appreciated
Ok, i fixed it with a bit of guesswork, but the problem seems to have gone away, but I'm not really sure it's "correct" per se. It seems to work, but I don't know if this is the most realistic way to emulate gravity and jumping because I'm not entirely sure how Time.deltaTime is affecting it. I understand it's purpose, but where I put it was a little bit of trial and error to see what worked at all. I moved the moveDir = new Vector3(...) line outside of the if grounded statement and changed zero to moveDir.y I'm not sure if that's correct, because it seems a little abstract to be putting it's own y component in its definition, but I think what it is doing is taking the y component from the last frame maybe? I'm not sure, but it works so who am I to question it. here's that section with the slight modification that I ended up using:
function Update()
{
moveDir = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDir.y, 0);
if (theController.isGrounded == true)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
{
moveDir.y = jumpHeight;
}
}
moveDir.y -= gravity * Time.deltaTime;
theController.$$anonymous$$ove(moveDir * Time.deltaTime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612000327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
CharacterMotor problem (Js) 1 Answer
2D Character Jumps only 1 time 2 Answers
How to make smooth jump? 1 Answer
Particular "Jump" Script. 2 Answers
unity is not responding 1 Answer