- Home /
How can i make my character fly with this controller ?
There's my controller:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public enum CameraStyle
{
ThirdPerson,
Action
}
public class NetworkInput : NetworkMovement
{
// Serialized Fields
[SerializeField, Tooltip("The default speed of the player.")] private float runSpeed = 5f;
[SerializeField, Tooltip("The sprinting (Left Shift) speed of the player.")] private float sprintSpeed = 7f;
[SerializeField, Tooltip("The crouch speed of the player.")] private float crouchSpeed = 3f;
[SerializeField, Tooltip("Allows player to run/sprint (Left Shift).")] private bool allowSprint = true;
[SerializeField, Tooltip("Allows player to crouch (Left CTRL).")] private bool allowCrouch = true;
[SerializeField, Tooltip("Attach the CharacterController of the player to here.")] private CharacterController _characterController;
[SerializeField, Tooltip("Mouse Character Rotation Sensitivity")] private float mouseSens = 100;
[SerializeField, Tooltip("Max Vertical Look Limit")] private float verticalMouseLookLimit = 170;
// Private Fields
private float _verticalSpeed = 0;
private bool _jump = false;
private GameObject _camera;
private Animator _animator;
private float currentSpeed;
private Player _player;
private InputField[] _ignoreFields;
// Public Fields
[Tooltip("The network rotation, used to determine network direction of player.")] public Quaternion pawnRotation;
[Tooltip("Snap distance for synchronizing player positions.")] public float _snapDistance = 1;
[Tooltip("How high the player jumps.")] public float _jumpHeight = 12f;
public bool mouseLocked = true;
private Player player;
public CameraStyle cameraStyle;
void Start()
{
if (cameraStyle == CameraStyle.Action)
MouseLock(true);
player = Utils.ClientLocalPlayer();
}
void LateUpdate()
{
// Always make sure _characterController is set.
if (!_characterController)
_characterController = GetComponent<CharacterController>();
}
public override void GetInputs(ref Inputs inputs)
{
InputField[] ignore = InputField.FindObjectsOfType<InputField>();
_ignoreFields = ignore;
for (int i = 0; i < _ignoreFields.Length; i++)
{
if (_ignoreFields[i].isFocused)
{
inputs.sides = 0;
inputs.forward = 0;
inputs.pitch = 0;
inputs.yaw = 0;
inputs.sprint = false;
inputs.crouch = false;
if(Cursor.lockState != CursorLockMode.None && cameraStyle == CameraStyle.Action)
MouseLock(false);
return;
} else
{
if (Cursor.lockState != CursorLockMode.Locked && cameraStyle == CameraStyle.Action)
MouseLock(true);
}
}
if(player.health <= 0)
{
inputs.sides = 0;
inputs.forward = 0;
inputs.pitch = 0;
inputs.yaw = 0;
inputs.sprint = false;
inputs.crouch = false;
return;
}
if (Input.GetKeyDown(KeyCode.LeftAlt) && cameraStyle == CameraStyle.Action)
{
if (mouseLocked)
MouseLock(false);
else
MouseLock(true);
}
if (cameraStyle == CameraStyle.Action)
{
if (player.health > 0)
{
inputs.sides = Input.GetAxis("Horizontal");
inputs.forward = Input.GetAxis("Vertical");
}
else
{
inputs.sides = 0;
inputs.forward = 0;
}
if (mouseLocked)
{
inputs.yaw = -Input.GetAxis("Mouse Y") * mouseSens * Time.fixedDeltaTime / Time.deltaTime;
inputs.pitch = Input.GetAxis("Mouse X") * mouseSens * Time.fixedDeltaTime / Time.deltaTime;
}
else
{
inputs.yaw = 0;
inputs.pitch = 0;
}
}
else if (cameraStyle == CameraStyle.ThirdPerson)
{
if (player.health > 0)
{
inputs.sides = Input.GetAxis("Horizontal");
inputs.forward = Input.GetAxis("Vertical");
}
else
{
inputs.sides = 0;
inputs.forward = 0;
}
if (Input.GetMouseButton(1))
{
inputs.yaw = -Input.GetAxis("Mouse Y") * mouseSens * Time.fixedDeltaTime / Time.deltaTime;
inputs.pitch = Input.GetAxis("Mouse X") * mouseSens * Time.fixedDeltaTime / Time.deltaTime;
}
else if (Input.GetButton("Rotate"))
{
inputs.yaw = 0f;
inputs.pitch = Input.GetAxis("Rotate") * mouseSens * Time.fixedDeltaTime / Time.deltaTime;
} else
{
inputs.yaw = 0f;
inputs.pitch = 0f;
}
}
inputs.sprint = Input.GetButton("Sprint");
inputs.crouch = Input.GetButton("Crouch");
float verticalTarget = -1;
if (_characterController.isGrounded)
{
if (Input.GetButton("Jump") && player.health > 0)
{
_jump = true;
}
inputs.vertical = 0;
verticalTarget = 0;
}
if (_jump)
{
verticalTarget = 1;
if (inputs.vertical >= 0.9f)
{
_jump = false;
}
}
foreach (var anim in GetComponentsInChildren<Animator>())
{
anim.SetFloat("Horizontal", inputs.sides);
anim.SetFloat("Vertical", inputs.forward);
anim.SetFloat("Speed", currentSpeed);
anim.SetBool("Crouch", inputs.crouch);
}
inputs.vertical = Mathf.Lerp(inputs.vertical, verticalTarget, 10 * Time.deltaTime);
}
public override Vector3 Move(Inputs inputs, Results current)
{
transform.position = current.position;
float speed = 4;
if (current.crouching)
{
speed = 3f;
}
if (current.sprinting)
{
speed = 6;
}
if (inputs.vertical > 0)
{
_verticalSpeed = inputs.vertical * _jumpHeight;
}
else
{
_verticalSpeed = inputs.vertical * Physics.gravity.magnitude;
}
_characterController.Move(transform.TransformDirection((Vector3.ClampMagnitude(new Vector3(inputs.sides, 0, inputs.forward), 1) * speed) + new Vector3(0, _verticalSpeed, 0)) * Time.fixedDeltaTime);
return transform.position;
}
public override Quaternion Rotate(Inputs inputs, Results current)
{
if (cameraStyle == CameraStyle.Action)
{
if (mouseLocked && !Utils.IsCursorOverUserInterface())
{
transform.rotation = current.rotation;
float mHor = current.rotation.eulerAngles.y + inputs.pitch * Time.fixedDeltaTime;
float mVert = current.rotation.eulerAngles.x + inputs.yaw * Time.fixedDeltaTime;
if (mVert > 180)
mVert -= 360;
mVert = Mathf.Clamp(mVert, -verticalMouseLookLimit * 0.5f, verticalMouseLookLimit * 0.5f);
transform.rotation = Quaternion.Euler(0, mHor, 0);
pawnRotation = Quaternion.Euler(mVert, mHor, 0);
}
}
else if (cameraStyle == CameraStyle.ThirdPerson)
{
if (!Utils.IsCursorOverUserInterface())
{
transform.rotation = current.rotation;
float mHor = current.rotation.eulerAngles.y + inputs.pitch * Time.fixedDeltaTime;
float mVert = current.rotation.eulerAngles.x + inputs.yaw * Time.fixedDeltaTime;
if (mVert > 180)
mVert -= 360;
mVert = Mathf.Clamp(mVert, -verticalMouseLookLimit * 0.5f, verticalMouseLookLimit * 0.5f);
transform.rotation = Quaternion.Euler(0, mHor, 0);
pawnRotation = Quaternion.Euler(mVert, mHor, 0);
}
}
return pawnRotation;
}
public override void UpdatePosition(Vector3 newPosition)
{
if ((newPosition - transform.position).sqrMagnitude < _snapDistance * _snapDistance)
{
transform.position = newPosition;
}
else
{
_characterController.Move(newPosition - transform.position);
}
}
public override void UpdateRotation(Quaternion newRotation)
{
transform.rotation = Quaternion.Euler(0, newRotation.eulerAngles.y, 0);
pawnRotation = newRotation;
}
public void MouseLock(bool b)
{
if (b)
{
mouseLocked = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
mouseLocked = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
}
Just gonna comment here. I haven't done a whole lot with flying controls and such, so other people would probably have better ideas than I do on this topic. However, that is a VERY general question. There are many different ways to make things "fly" and we aren't sure what exactly you're looking for. Also, posting a large chunk of code like can turn people away from helping sometimes because it is a lot for them to dig through. Having a more focused, specific question, and a smaller, more targeted code segment focusing on the specific bit of code you are working with generally helps things move along nicely.
In addition, if you don't know how to use code by reading it, you should really study it more. If you're still stuck, you may need to spend more time working on basic program$$anonymous$$g concepts.
The community loves to help fix weird bugs, but we can't cover for a lack of individual skill.
Your answer
![](https://koobas.hobune.stream/wayback/20220612172754im_/https://answers.unity.com/themes/thub/images/avi.jpg)