- Home /
Jump with Character Controller
I'm sorry if this is a stupid question, but I'm trying to figure out how to make a character jump using the character controller. I've been looking through the documentation, googling, looking through this forum, and watching youtube videos for about 3 hours now, and none of the stuff I'm finding is helping. I have a cube in my game that can walk around by rotating and moving forward, but how on earth do I make it jump with space? I've copy pasted my entire code below; please keep in mind that I am an absolute beginner to Unity and to coding in general. I got most of this from a youtube video, so a lot of it looks sloppy to me and I'm not entirely sure what all of it does... I just don't know what to put for when jump is true.
using UnityEngine; using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float rotateSpeed;
public float jumpSpeed;
CharacterController cc;
private bool jump;
void Start ()
{
cc = GetComponent<CharacterController> ();
}
void Update()
{
Vector3 forward = Input.GetAxis ("Vertical") * transform.TransformDirection (Vector3.forward)*moveSpeed;
transform.Rotate (new Vector3(0.0f, (Input.GetAxis ("Horizontal")*rotateSpeed*Time.deltaTime), 0.0f));
cc.Move (forward * Time.deltaTime);
cc.SimpleMove (Physics.gravity*Time.deltaTime);
if (Input.GetKeyDown (KeyCode.Space))
{
jump = true;
}
if (jump)
{
}
}
}
Answer by Gixtox · May 21, 2015 at 08:59 PM
So if I am correct it looks like you check to see if the Space bar is pressed and then do nothing with that. You say if space is pressed then jump is true. But then you say if jump is true, nothing. Add something into your jump if statement like a force.
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Add a force in the right direction and your cube should jump. If you want to go a step further you can cut out the jump = true; and just put an Addforce in your first if statement.
Right but like I said I don't know what to put in the if (jump), I've just copied this mostly from videos and tutorials. I'm not using a Rigidbody, I'm using a character controller, and I might be wrong but I don't think you can use Addforce with a character controller
So I looked at the character controller script reference here.
http://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html
So I've never used a character controller but here is how I'm understanding it. If you look at their code and your code, they use controller.$$anonymous$$ove while you use cc.$$anonymous$$ove. So all you need to do is put that into your if last if statement. It should be something like
cc.$$anonymous$$ove(0,jumpSpeed*Time.deltaTime,0);
I've made a few changes, and now it jumps when you press space so I think I'm getting somewhere BUT it keeps jumping. it just goes up and down forever. This is what I have:
public class PlayerController : $$anonymous$$onoBehaviour {
public float moveSpeed;
public float rotateSpeed;
public float jumpSpeed;
CharacterController cc;
private bool jump;
void Start ()
{
cc = GetComponent<CharacterController> ();
}
void Update()
{
Vector3 forward = Input.GetAxis ("Vertical") * transform.TransformDirection (Vector3.forward)*moveSpeed;
transform.Rotate (new Vector3(0.0f, (Input.GetAxis ("Horizontal")*rotateSpeed*Time.deltaTime), 0.0f));
cc.$$anonymous$$ove (forward * Time.deltaTime);
cc.Simple$$anonymous$$ove (Physics.gravity*Time.deltaTime);
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
jump = true;
}
if(jump)
{
Vector3 jumpUp = transform.TransformDirection(Vector3.up)*jumpSpeed;
cc.$$anonymous$$ove (jumpUp * Time.deltaTime);
}
}
}
You never set jump back to false. Add
jump = false;
to the end of the if statement.
Thank you! now I just have to make it look less clunky.
Your answer
Follow this Question
Related Questions
[C#] Jump on slopes 1 Answer
Jump and move (CharacterController.velocity) C# 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
My Player can't jump - coding 1 Answer
Need helping getting a character controller to jump 1 Answer