using UnityEngine;
using System.Collections;
// basic WASD-style movement control
// commented out line demonstrates that transform.Translate instead of charController.Move doesn't have collision detection
[RequireComponent(typeof(CharacterController))]
[AddComponentMenu("Control Script/FPS Input")]
public class FPSInput : MonoBehaviour
{
public float speed = 6.0f;
public float gravity = -9.8f;
public float jumpSpeed = 8.0f;
private CharacterController _charController;
private Vector3 moveDirection = Vector3.zero;
private bool grounded = false;
void Start()
{
_charController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
if (grounded)
//we are grounded, So recalculate moved direction directly from axes
if (Input.GetButton("jump")) moveDirection.y = jumpSpeed;
}
void Update()
{
//transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);
movement.y = gravity;
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charController.Move(movement);
}
}
From my comment on the main post. Try adding the jump to Update, and applying the value to movement.y
using UnityEngine;
using System.Collections;
// basic WASD-style movement control
// commented out line demonstrates that transform.Translate instead of charController.Move doesn't have collision detection
[RequireComponent(typeof(CharacterController))]
[AddComponentMenu("Control Script/FPS Input")]
public class FPSInput : MonoBehaviour
{
public float speed = 6.0f;
public float gravity = -9.8f;
public float jumpSpeed = 8.0f;
private CharacterController _charController;
private Vector3 moveDirection = Vector3.zero;
private Vector3 movement = Vector3.zero;
private bool grounded = false;
void Start()
{
_charController = GetComponent<CharacterController>();
}
//FixedUpdate removed.
void Update()
{
//transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
movement = new Vector3(deltaX, movement.y, deltaZ);
if (grounded)
//we are grounded, So recalculate moved direction directly from axes
if (Input.GetButton("jump"))
movement.y = jumpSpeed;
// Not needed as x/y will always be less then speed unless a frame is greater then one second, as the input value is always 0-1.
// movement = Vector3.ClampMagnitude(movement, speed);
movement.y = Mathf.Clamp(movement.y + gravity * Time.delatTime, gravity, float.PositiveInfinity);
movement.x *= Time.deltaTime;
movement.z *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charController.Move(movement);
}
}
Note had to make movement a class variable so the y value would keep through frames, and also apply gravity over time so the jump speed would decrease over time, letting the player fall.