I have this issue now for days and it is driving me crazy…
Using a very simple Charactercontroller with this snippet:
using UnityEngine;
using System.Collections;
public class FPS : MonoBehaviour {
public float speed = 20.0f;
public float jumpSpeed = 10.0f;
public float gravity = 25.0f;
public float mouseSensitivity = 5f;
public float upDownRange = 45f;
private Vector3 moveDirection = Vector3.zero;
private bool grounded = false;
private float verticalRotation = 0f;
void Update () {
transform.Rotate (0, Input.GetAxis ("Mouse X") * mouseSensitivity, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
}
void FixedUpdate() {
if (grounded) {
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;
CharacterController controller = GetComponent<CharacterController>();
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
if (transform.position.y < 0) {
transform.position = new Vector3 (transform.position.x, 256, transform.position.z);
}
}
}
But instead of a nice jump, the char only jumps when standing still - when moving around the char gets boosted into the direction it is heading and only going up when it hits a wall ?? I tried EVERYTHING millions of different approaches but the outcome is the same.
To test this just:
- make a scene, add a plane
- add a cube or something
- add charactercontroller to the cube
- attach the camera to the cube
- add this script in the post to the cube
Walk and ‘jump’…
Is this a bug?