The object floats up as I hold spacebar, then floats back down. We are using rigided2D box Collides.
Here is the code.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton(“Jump”))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Since you’re using Input.GetButton, it is constantly polling for that button. You might be looking for Input.GetButtonDown or Input.GetButtonUp
Can’t seem to get this to work, is there another way I could write this code.
Not sure if this will help… but here is some old code that I used while prototyping my latest project…
I looked through your code and it seemed a bit off, I didn’t have a whole lot of time haha. Sorry. I’ll take a second look in a bit. but I had this project open so I figured I could post some old code.
//Gives that initial push, all handled through the rigidbody2D.
//Jump being an adjustable float.
//Jump mod tells the game to start running the code that adjusts height of jump.
if(Input.GetButtonDown("Jump"))
{
if(isGrounded)
{
this.rigidbody2D.velocity += new Vector2(0,Jump);
isGrounded = false;
JumpModActive = true;
}
}
//While the button is held, this adds more velocity to the jump... Making the jump height controlled by player.
if(JumpModActive)
{
JumpModtime += Time.deltaTime;
if(JumpModtime >= JumpModTime)
{
JumpModActive = false;
JumpModtime = 0;
}
this.rigidbody2D.AddForce(new Vector2(0,JumpModifier * Time.deltaTime));
}
//This controls how high the jump is. Based on how long the player holds jump down.
if(Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.UpArrow))
{
JumpModActive = false;
}
Here is how I got figured if it was grounded… Not the best way. You could do a few different things to get this working better. Like having colliders on the feet of the character trigger this. So it KNEW it was on the ground.
void OnCollisionEnter2D()
{
isGrounded = true;
}
I still get the same results with your code, sorry for the late response ended up in the hospital with blood poisoning…
Bump, I can not figure this out. My object just keeps floating up when I hold the key. Then it floats back down…
Hmmm, my code if not setup properly will give that effect. However its setup so the jump height is modified by how long you hold down the jump button. If you want you can rip that section out and try it with just the most basic jump script. Not sure if you want jump height to be modified but it will help narrow down the problem.
Just rip out the “if(JumpModActive)” section of my code.
Sorry for the late response as two of the servers in my rack failed so I spent much of the last two days getting hardware replacements and getting them back online. Also, I hope you’re doing better after being in the hospital.