- Home /
Question by
Bokaii · Dec 06, 2015 at 11:47 AM ·
c#movement scriptcharacter controller
Character controller air control
So I wrote this movement script, using a character controller. I want to be able to move a tiny bit, while in the air. (like Minecraft, Counter strike etc.) But don't know how to change the movement control when in the air.
Here's the script:
using UnityEngine;
using System.Collections;
public class FPSController : MonoBehaviour {
//BOOLS
public bool grounded = true;
//FLOATS
public float speed = 5;
public float airAcceleration = 0.3f;
public float jumpSpeed = 2;
public float gravity = 35;
//VECTORS
public Vector3 moveDirection = Vector3.zero;
//OTHERS
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
controller.Move(moveDirection * Time.deltaTime);
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
}
else
{
moveDirection.y -= gravity * Time.deltaTime;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can only move character left or right, no foward or backwards. 1 Answer
How can I make the player step forward on mouse click? 2 Answers
My character moves when i dont want to 0 Answers