1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| using UnityEngine;
using System;
using System.Collections;
using System.ComponentModel;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;
public class Movement : MonoBehaviour
{
private Device joystick;
public float MoveDirectVitesse;
public float GaucheDroiteVitesse;
public Movement()
{
foreach (DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick==null)
{
throw new Exception("Aucun joystick branché");
}
foreach (DeviceObjectInstance doi in joystick.Objects)
{
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(ParameterHow.ById,doi.ObjectId,new InputRange(-50, 50));
}
}
joystick.Properties.AxisModeAbsolute = true;
//joystick.SetDataFormat (DeviceDataFormat.Joystick);
//joystick.SetCooperativeLevel(CooperativeLevelFlags.Exclusive);
joystick.Acquire();
JoystickState state = joystick.CurrentJoystickState;
MoveDirectVitesse=state.X;
GaucheDroiteVitesse=state.Y;
}
void Start()
{
}
// Update is called once per frame
void Update ()
{
// Amount to Move
float direct = Input.GetAxis("Vertical") * MoveDirectVitesse * Time.deltaTime;
float gauchedroite = Input.GetAxis("Horizontal") * GaucheDroiteVitesse * Time.deltaTime;
// Move the player
transform.Translate(UnityEngine.Vector3.back * direct);
transform.Translate(UnityEngine.Vector3.left* gauchedroite);
}
} |
Partager