Sorry for the long post, but I’ll post the code I have.
I appreciate your help, thank you very much. I only pasted the actual code, the top portion of the scripts just make it longer.
Mobile Input
public override void Init()
{
base.Init();
touchArea = new Vector2(100, 100);
//Setup Sticky GUI Texture
sticky.pixelInset = new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.1f, Screen.width * 0.1f);
stickyWidth = sticky.pixelInset.width / 2;
stickyHeight = sticky.pixelInset.height / 2;
btShot.pixelInset = new Rect((Screen.width - Screen.width * 0.1f) - Screen.width * 0.05f, (Screen.width * 0.1f) - Screen.height * 0.1f, Screen.width * 0.1f, Screen.width * 0.1f);
btTurbo.pixelInset = new Rect((Screen.width - Screen.width * 0.1f) - Screen.width * 0.02f, (Screen.width * 0.1f) + Screen.height * 0.15f, Screen.width * 0.1f, Screen.width * 0.1f);
}
public override void Stop()
{
base.Stop();
}
// Update is called once per frame
void Update()
{
if (accelerometer)
{
CheckMovementTilt();
}
else
{
if (Input.touchCount > 0)
{
CheckMovement();
CheckButtons();
}
else
{
pitch = Mathf.Lerp(pitch, 0, Time.deltaTime * 5);
yaw = Mathf.Lerp(yaw, 0, Time.deltaTime * 5);
ResetSticky();
ResetShot();
}
}
}
private void CheckMovementTilt()
{
pitch = Input.acceleration.x;
yaw = -Input.acceleration.y;
if (Mathf.Abs(pitch) * 10 < pitchOffset)
{
pitch = 0;
}
if (Mathf.Abs(yaw) * 10 < yawOffset)
{
yaw = 0;
}
}
private void CheckMovement()
{
Touch t = Input.GetTouch(0);
bool moveTouch = false;
foreach (Touch touch in Input.touches)
{
if (touch.position.x < Screen.width / 2)
{
t = touch;
GetInitTouchPosition(t);
moveTouch = true;
break;
}
}
if (!moveTouch)
{
pitch = Mathf.Lerp(pitch, 0, Time.deltaTime * 5);
yaw = Mathf.Lerp(yaw, 0, Time.deltaTime * 5);
ResetSticky();
return;
}
myDelta = initPosition - t.position;
myDelta.x = Mathf.Clamp(myDelta.x / touchArea.x, -1, 1) * -1;
myDelta.y = Mathf.Clamp(myDelta.y / touchArea.y, -1, 1) * -1;
pitch = -myDelta.y;
yaw = myDelta.x;
Vector2 tPos = new Vector2(initPosition.x + (myDelta.x * touchArea.x) - stickyWidth,
initPosition.y + (myDelta.y * touchArea.y) - stickyHeight);
sticky.pixelInset = new Rect(tPos.x, tPos.y, sticky.pixelInset.width, sticky.pixelInset.height);
//sticky.enabled = true;
sticky.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
}
private void GetInitTouchPosition(UnityEngine.Touch touch)
{
if (touch.phase == TouchPhase.Began)
{
initPosition = touch.position;
}
}
private void CheckButtons()
{
UnityEngine.Touch t;
bool isPressingShot =false,isPressingTurbo=false;
bool rightSideTouch =false;
foreach (UnityEngine.Touch touch in Input.touches)
{
if (touch.position.x > Screen.width / 2)
{
t = touch;
rightSideTouch = true;
if (btShot.HitTest(t.position))
{
shot = true;
isPressingShot = true;
btShot.color = new Color(0.5f, 0.5f, 0.5f, 0.5f); ;
}
else
{
if (!isPressingShot)
ResetShot();
}
if (btTurbo.HitTest(t.position) t.phase == TouchPhase.Began)
{
turbo = !turbo;
}
if(turbo)
{
isPressingTurbo = true;
btTurbo.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
}
else
{
if (!isPressingTurbo)
ResetTurbo();
}
}
}
if (!rightSideTouch)
{
ResetShot();
}
}
private void ResetSticky()
{
//sticky.enabled = false;
sticky.color = new Color(0.5f, 0.5f, 0.5f, 38f / 255f);
sticky.pixelInset = new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.1f, Screen.width * 0.1f);
}
private void ResetShot()
{
shot = false;
btShot.color = new Color(0.5f, 0.5f, 0.5f, 38f / 255f);
}
private void ResetTurbo()
{
turbo = false;
btTurbo.color = new Color(0.5f, 0.5f, 0.5f, 38f / 255f);
}
}
Also there is “ShipControl”
void Start()
{
SelectInput();
}
// Update is called once per frame
void Update()
{
RotateShipMesh();
Shot();
}
// Update is called once per frame
void FixedUpdate()
{
CalculateSpeed();
if(_input.turbo)
rigidbody.velocity = transform.forward * turboSpeed;
else
rigidbody.velocity = transform.forward * speed;
_inputForce = (Vector3.right * _input.pitch * pitchMultiplier) + (Vector3.up * _input.yaw * yawMultiplier);
transform.Rotate(_inputForce * Time.deltaTime, Space.Self);
Debug.DrawLine(transform.position, transform.position + _inputForce * 5);
}
void CalculateSpeed()
{
float x, y, z;
x = Vector3.Dot(rigidbody.velocity, transform.forward);
y = Vector3.Dot(rigidbody.velocity, transform.right);
z = Vector3.Dot(rigidbody.velocity, transform.up);
currentSpeed = new Vector3(x, y, z).magnitude * 2.237f;
}
void RotateShipMesh()
{
Vector3 rot = mesh.transform.localEulerAngles;
rot.y = _input.yaw * 20;
rot.x = _input.pitch * 20;
rot.z = 45 - _input.yaw * 15;
mesh.transform.localEulerAngles = rot;
}
void Shot()
{
shotTimer += Time.deltaTime;
if (_input.shot)
{
//Enough time passed to shot next missile?
if (shotTimer > shotRate)
{
GameObject shot = (GameObject)Instantiate(shotPrefab, transform.position, transform.rotation);
shotTimer = 0;
}
}
}
void OnCollisionEnter(Collision p_coll)
{
}
void OnTriggerEnter(Collider p_other)
{
}
/// <summary>
/// Select correct input method based on platform
/// </summary>
private void SelectInput()
{
if (Application.isEditor)
{
if(ApplicationManager.Instance.isTestingRemote)
_input = GetComponentInChildren<MobileInput>();
else
_input = GetComponentInChildren<KeyboardInput>();
}
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
_input = GetComponentInChildren<MobileInput>();
}
else
{
_input = GetComponentInChildren<KeyboardInput>();
}
_input.Init();
}
}