I have 3 scripts that serve as a control for a helicopter to run, the problem is that I changed the project from pc to android, and I need to transfer these keyboard controls to gui, but I can’t do that, and I do not even know if it’s possible .
I also tried to simulate the pressing of a key on the keyboard by pressing on the gui, but unity does not allow this.
Anyway, can anyone give me any idea how to do this?
This is the main thing.
He’s big but it’s only in the end that he has the useful part, I put it all together just so that someone needs to use it or have a base.
using UnityEngine;
using UnityEngine.UI;
public class HelicopterController : MonoBehaviour
{
public AudioSource HelicopterSound;
public ControlPanel ControlPanel;
public Rigidbody HelicopterModel;
public HeliRotorController MainRotorController;
public HeliRotorController SubRotorController;
public float TurnForce = 3f;
public float ForwardForce = 10f;
public float ForwardTiltForce = 20f;
public float TurnTiltForce = 30f;
public float EffectiveHeight = 100f;
public float turnTiltForcePercent = 1.5f;
public float turnForcePercent = 1.3f;
private float _engineForce;
public float EngineForce
{
get { return _engineForce; }
set
{
MainRotorController.RotarSpeed = value * 80;
SubRotorController.RotarSpeed = value * 40;
HelicopterSound.pitch = Mathf.Clamp(value / 40, 0, 1.2f);
if (UIGameController.runtime.EngineForceView != null)
UIGameController.runtime.EngineForceView.text = string.Format("Engine value [ {0} ] ", (int)value);
_engineForce = value;
}
}
private Vector2 hMove = Vector2.zero;
private Vector2 hTilt = Vector2.zero;
private float hTurn = 0f;
public bool IsOnGround = true;
// Use this for initialization
void Start ()
{
ControlPanel.KeyPressed += OnKeyPressed;
}
void Update () {
}
void FixedUpdate()
{
LiftProcess();
MoveProcess();
TiltProcess();
}
private void MoveProcess()
{
var turn = TurnForce * Mathf.Lerp(hMove.x, hMove.x * (turnTiltForcePercent - Mathf.Abs(hMove.y)), Mathf.Max(0f, hMove.y));
hTurn = Mathf.Lerp(hTurn, turn, Time.fixedDeltaTime * TurnForce);
HelicopterModel.AddRelativeTorque(0f, hTurn * HelicopterModel.mass, 0f);
HelicopterModel.AddRelativeForce(Vector3.forward * Mathf.Max(0f, hMove.y * ForwardForce * HelicopterModel.mass));
}
private void LiftProcess()
{
var upForce = 1 - Mathf.Clamp(HelicopterModel.transform.position.y / EffectiveHeight, 0, 1);
upForce = Mathf.Lerp(0f, EngineForce, upForce) * HelicopterModel.mass;
HelicopterModel.AddRelativeForce(Vector3.up * upForce);
}
private void TiltProcess()
{
hTilt.x = Mathf.Lerp(hTilt.x, hMove.x * TurnTiltForce, Time.deltaTime);
hTilt.y = Mathf.Lerp(hTilt.y, hMove.y * ForwardTiltForce, Time.deltaTime);
HelicopterModel.transform.localRotation = Quaternion.Euler(hTilt.y, HelicopterModel.transform.localEulerAngles.y, -hTilt.x);
}
private void OnKeyPressed(PressedKeyCode[] obj)
{
float tempY = 0;
float tempX = 0;
// stable forward
if (hMove.y > 0)
tempY = - Time.fixedDeltaTime;
else
if (hMove.y < 0)
tempY = Time.fixedDeltaTime;
// stable lurn
if (hMove.x > 0)
tempX = -Time.fixedDeltaTime;
else
if (hMove.x < 0)
tempX = Time.fixedDeltaTime;
foreach (var pressedKeyCode in obj)
{
switch (pressedKeyCode)
{
case PressedKeyCode.SpeedUpPressed:
EngineForce += 0.1f;
break;
case PressedKeyCode.SpeedDownPressed:
EngineForce -= 0.12f;
if (EngineForce < 0) EngineForce = 0;
break;
case PressedKeyCode.ForwardPressed:
if (IsOnGround) break;
tempY = Time.fixedDeltaTime;
break;
case PressedKeyCode.BackPressed:
if (IsOnGround) break;
tempY = -Time.fixedDeltaTime;
break;
case PressedKeyCode.LeftPressed:
if (IsOnGround) break;
tempX = -Time.fixedDeltaTime;
break;
case PressedKeyCode.RightPressed:
if (IsOnGround) break;
tempX = Time.fixedDeltaTime;
break;
case PressedKeyCode.TurnRightPressed:
{
if (IsOnGround) break;
var force = (turnForcePercent - Mathf.Abs(hMove.y))*HelicopterModel.mass;
HelicopterModel.AddRelativeTorque(0f, force, 0);
}
break;
case PressedKeyCode.TurnLeftPressed:
{
if (IsOnGround) break;
var force = -(turnForcePercent - Mathf.Abs(hMove.y))*HelicopterModel.mass;
HelicopterModel.AddRelativeTorque(0f, force, 0);
}
break;
}
}
hMove.x += tempX;
hMove.x = Mathf.Clamp(hMove.x, -1, 1);
hMove.y += tempY;
hMove.y = Mathf.Clamp(hMove.y, -1, 1);
}
private void OnCollisionEnter()
{
IsOnGround = true;
}
private void OnCollisionExit()
{
IsOnGround = false;
}
}
using System;
using System.Collections.Generic;
using UnityEngine;
public class ControlPanel : MonoBehaviour {
public AudioSource MusicSound;
[SerializeField]
KeyCode SpeedUp = KeyCode.Space;
[SerializeField]
KeyCode SpeedDown = KeyCode.C;
[SerializeField]
KeyCode Forward = KeyCode.W;
[SerializeField]
KeyCode Back = KeyCode.S;
[SerializeField]
KeyCode Left = KeyCode.A;
[SerializeField]
KeyCode Right = KeyCode.D;
[SerializeField]
KeyCode TurnLeft = KeyCode.Q;
[SerializeField]
KeyCode TurnRight = KeyCode.E;
[SerializeField]
KeyCode MusicOffOn = KeyCode.M;
private KeyCode[] keyCodes;
public Action<PressedKeyCode[]> KeyPressed;
private void Awake()
{
keyCodes = new[] {
SpeedUp,
SpeedDown,
Forward,
Back,
Left,
Right,
TurnLeft,
TurnRight
};
}
void Start () {
}
void FixedUpdate ()
{
var pressedKeyCode = new List<PressedKeyCode>();
for (int index = 0; index < keyCodes.Length; index++)
{
var keyCode = keyCodes[index];
if (Input.GetKey(keyCode))
pressedKeyCode.Add((PressedKeyCode)index);
}
if (KeyPressed != null)
KeyPressed(pressedKeyCode.ToArray());
// for test
if (Input.GetKey(MusicOffOn))
{
if ( MusicSound.volume == 1) return;
/* if (MusicSound.isPlaying)
MusicSound.Stop();
else*/
MusicSound.volume = 1;
MusicSound.Play();
}
}
}
public enum PressedKeyCode
{
SpeedUpPressed,
SpeedDownPressed,
ForwardPressed,
BackPressed,
LeftPressed,
RightPressed,
TurnLeftPressed,
TurnRightPressed
}
Thanks to anyone who can help me. <3