Question by
Numonic · Nov 30, 2016 at 04:46 AM ·
3rd person controller
3rd Person Character Controller with Flying Ability
Hi Im working on a 3rd person character controller that has the ability to fly think War for Cybertron transformers.
I want to be able to disable gravity and have the character fly. Worked on this line of code. But not having the results I expected. Any help would be appreciated.
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
CharacterController characterController;
bool isJumping = true;
bool isMoving = true;
bool isFlight = false;
bool isGrounded = true;
public float speed = 10.0f;
public float flightSpeed = 2.0f;
public float rotationSpeed = 2.0f;
[SerializeField]
float moveSpeed = 5.0f;
[SerializeField]
float _jumpSpeed = 20.0f; // How high we can jump
[SerializeField]
float _gravity = 1.0f;
float _yVelocity = 0.0f; // set to jump speed
// Use this for initialization
void Start ()
{
characterController = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
Moving ();
Jumping ();
Flight ();
}
void Moving()
{
Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), flightSpeed, Input.GetAxis("Vertical"));
Vector3 velocity = direction * moveSpeed;
velocity.y = _yVelocity;
velocity = transform.TransformDirection (velocity);
characterController.Move (velocity * Time.deltaTime);
}
void Jumping()
{
if (characterController.isGrounded)
{
if (Input.GetButtonDown ("Jump"))
{
_yVelocity = _jumpSpeed;
}
}
else
{
_yVelocity -= _gravity;
}
}
void Flight ()
{
if (Input.GetMouseButtonDown (0))
{
if (characterController.isGrounded)
{ transform.Translate (0, +flightSpeed * Time.deltaTime, 0);
print ("We are flying");
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Hi , thanks for the nice welcome. 0 Answers
Need Help with char controller and animation 0 Answers
Unity input stutter with new Input System and 3rd Person Starter Asset 2 Answers
Unexpected symbol `)' 0 Answers