- Home /
Question by
yellowyears · May 07, 2020 at 07:42 PM ·
c#unity 5movementphysicscharactercontroller
How do I make my character controller jump when I use this code?
It's for unity xr but that should be irrelevant. I've no idea how to do this and most tutorials don't work because of the way my movement is setup.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
public class MovementProvider : LocomotionProvider
{
public float speed = 1.0f;
public float gravityMultiplier = 1.0f;
// We make this list so we can check for input
public List<XRController> controllers = null;
private CharacterController characterController = null;
private GameObject head = null; // Basically the camera.
protected override void Awake()
{
characterController = GetComponent<CharacterController>();
head = GetComponent<XRRig>().cameraGameObject;
}
private void Start()
{
PositionController();
}
private void FixedUpdate()
{
PositionController();
CheckForInput();
ApplyGravity();
}
private void PositionController()
{
// This method basically moves the capsule with the camera.
// This is important when using a roomscale vr rig, because if you aren't in the middle of your playspace,
// the camera will get stuck on things.
// Get head height, we clamp it so we have a min & max value, so they can't walk through doorways or shrink too small.
float headHeight = Mathf.Clamp(head.transform.localPosition.y, 1, 2);
characterController.height = headHeight;
// Cut in half and add the skin
Vector3 newCentre = Vector3.zero;
newCentre.y = characterController.height / 2;
newCentre.y += characterController.skinWidth;
// Move the capsule in local space
newCentre.x = head.transform.localPosition.x;
newCentre.z = head.transform.localPosition.z;
// Apply all of this
characterController.center = newCentre;
}
private void CheckForInput()
{
// Loop through the controller list and then check if the input is enabled.
foreach(XRController controller in controllers)
{
// If the input is enabled
if(controller.enableInputActions)
{
CheckForMovement(controller.inputDevice);
CheckForJump(controller.inputDevice);
}
}
}
private void Jump(bool isClicked)
{
if(isClicked && characterController.isGrounded)
{
}
}
private void CheckForJump(InputDevice device)
{
if(device.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out bool isClicked))
{
Jump(isClicked);
}
}
private void CheckForMovement(InputDevice device)
{
if (device.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 position)) // Primary 2D axis refers to the joystick or touchpad.
{
StartMove(position);
}
}
private void StartMove(Vector2 position)
{
// Apply touch position to head forward vector
// Get the direction the player wants to go
Vector3 direction = new Vector3(position.x, 0, position.y);
// Get direction user is looking
Vector3 headRotation = new Vector3(0, head.transform.eulerAngles.y, 0);
// Rotate input direction by horizontal head rotation
direction = Quaternion.Euler(headRotation) * direction; // Convert to quaternion
// Apply speed and move
Vector3 movement = direction * speed;
characterController.Move(movement * Time.deltaTime);
}
private void ApplyGravity()
{
Vector3 gravity = new Vector3(0, Physics.gravity.y * gravityMultiplier, 0);
gravity.y *= Time.deltaTime;
characterController.Move(gravity * Time.deltaTime);
}
}
Comment