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 3A. Jumping - MAKE A GAME (Unity) - YouTube (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;
}
}