- Home /
The question is answered, right answer was accepted
Questions regarding jumping
I know this is really easy stuff, but I coudn't find a definitive answer, so I thought I should ask. It's an odd combination but I'm trying to learn how to create a basic character controller, by using Brackeys https://www.youtube.com/watch?v=PazLGgeFkHI (translating the code to C#) and the CharacterController2D you can find in the official documentation, only taking the basic concepts to 3D. Now you see, unlike the video ,where the ball actually jumps with the force, my cube just blinks a couple of coordinates up, without all the smoothness. My code should work the same way, even though I'm using rigidbody.addforce instead of velocity, but neither works anyway. I would really like to hear any suggestions. Thanks in advance! My code:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
private Vector3 movement;
private float jumpSpeed = 8.0f;
private bool fallin = false;
public Vector3 speed = new Vector3(10, 0, 10);
void Update () {
float inputX = Input.GetAxis ("Horizontal");
float inputZ = Input.GetAxis ("Vertical");
movement = new Vector3 (speed.x * inputX, 0, speed.z * inputZ);
rigidbody.velocity = movement;
if (Input.GetKeyDown(KeyCode.Space) && fallin == false) {
rigidbody.AddForce(Vector3.up * jumpSpeed, ForceMode.VelocityChange);
fallin = true;
}
}
void OnCollisionStay() {
fallin = false;
}
}
It still just blinks to the coordinate ins$$anonymous$$d of going to it smoothly, like it should.
Answer by meat5000 · Feb 19, 2015 at 12:44 AM
You are zeroing out your jump velocity on the next frame.
Change the line to the following:
movement = new Vector3 (speed.x * inputX, rigidbody.velocity.y, speed.z * inputZ);
I tried your script and it barely got off the ground. With the new line it was smooth as silk. If this doesn't work there is something wrong which is not contained within that script.