I got this tank controller from the asset store and I am trying to make it run on my Tab. The problem is I don’t know how to convert the input from axis (WASD, arrow keys) to touches/gui buttons with multitouch; Here’s the code (not giving away anything here as the actual script for tank controller is separate; this is mere an input script)
public Texture btnTexture;
public bool enableUserInput = true;
public float steerG = 0.0f;
public float accelG = 0.0f;
void Update(){
if(enableUserInput){
accelG = Input.GetAxis("Vertical");
steerG = Input.GetAxis("Horizontal");
}
}
void FixedUpdate(){
//float accelerate = 0;
//float steer = 0;
UpdateWheels(accelG,steerG);
}
void OnGui (){
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
Debug.Log("I was supposed to accelerate");
}
The problem is I don’t even get the button rect on screen, is not drawn let alone make it run amok on the map. Please advise.
1 Answer
1
Not exactly an answer - everything works fine, buttons stay in place regardless the screen rezolution, multitouch works - however this weird behavior occurs: every other control (DriveBackwards and Right now) won’t work. I added Right code first, worked, added Left - left worked right stopped to work. Do I have a typo somewhere or is worse?
using UnityEngine;
using System.Collections;
public class PlayerTracksController : TankTracksController {
public float steerG = 0.0f;
public float accelG = 0.0f;
public GUITexture buttonTurnRight;
public GUITexture buttonTurnLeft;
public GUITexture buttonDriveBackwards;
public GUITexture buttonDrive;
public bool Right = false;
public bool Left = false;
public bool DriveBackwards = false;
public bool Drive = false;
void MovementControl(Vector2 position)
{
if (buttonTurnRight.GetScreenRect().Contains(position))
{
Right = true;
}
if (buttonTurnLeft.GetScreenRect().Contains(position))
{
Left = true;
}
if (buttonDriveBackwards.GetScreenRect().Contains(position))
{
DriveBackwards = true;
}
if (buttonDrive.GetScreenRect().Contains(position))
{
Drive = true;
}
}
void Update(){
Right = false;
Left = false;
DriveBackwards = false;
Drive = false;
for (int i=0; i<Input.touchCount; i++)
{
MovementControl(Input.touches*.position);*
}
}
}
I think i just wrote the gui funcion inside the fixed update :))
– CallToAdventureI made it work - using fixed steering and acceleration values (w I can play with later); works on the device too - only the buttons are not exactly multitouch. Let's search this site then :)
– CallToAdventure