character gravity with icade controller?

Hi,

I’m working on making a character controlled by a Bluetooth iCade controller (and relatively new to coding). They move, however I can’t seem to get the gravity aspect to work… When they should fall, they keep on walking in the air. This script is attached to a simple character. Here’s the code:

using UnityEngine;
using System.Collections;

public class ControllerInput : MonoBehaviour
{

** public float gravity = 10F;**
** private Vector3 moveDirection = Vector3.zero;**

void Start ()
{
iCadeBinding.setActive( true );

}

void Update ()
{
iCadeBinding.updateState();
Control();
}

void Control()
{
if (iCadeBinding.state.JoystickUp)
{
transform.position += Vector3.forward * 1 * Time.deltaTime;
}
else if (iCadeBinding.state.JoystickDown)
{
transform.position += Vector3.forward * -1 * Time.deltaTime;
}

if (iCadeBinding.state.JoystickLeft)
{
transform.position += Vector3.right * -1 * Time.deltaTime;
}
else if (iCadeBinding.state.JoystickRight)
{
transform.position += Vector3.right * 1 * Time.deltaTime;
}

__ moveDirection.y -= gravity * Time.deltaTime;__
}

}

Any recommendations on how to make this work? Thanks for your help! :slight_smile:

I would start with the standard character controller, and write an input wrapper class that will return the proper input depending on platform and game settings.

Keeping your character controller logic separate from your input handling will make both pieces of code easier to re-use. I would recommend against handling gravity and dealing directly with iCadeBinging in the same class.

Your gravity isn’t working in this case because you are applying it to your moveDirection vector, but never applying that to your transform.position. You will also need logic to detect when your character is on the ground and deal with all other types of collisions. This is not a trivial task which is why I’d recommend using the existing character controllers as a starting point (there is an existing CharacterController class as well as a new rigidbody-based on introduced with the new Unity Sample Assets on the asset store).