I’m not 100% sure how to move my char on the phone. Building a shooter app. I have it working for controls and keyboard and mouse. Now I need to get it for touch screens.
code:
// Update is called once per frame
void Update()
{
//HUD
_Score.text = Game_Data._Score.ToString();
_Kill_Count.text = Game_Data._Kill_Count.ToString();
//Ship Movement
if (Game_Data._Movement_Use_Mouse)
{
TargetFollow = Input.mousePosition;
TargetFollow = Camera.main.ScreenToWorldPoint(TargetFollow);//Convert Mouse Postion To World Postion
TargetFollow.z = transform.position.z;
transform.position = Vector3.Lerp(transform.position, TargetFollow, speed * Time.deltaTime); // Move object to pointer
}
else
{
rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
}
//Fire Weapon
if (Game_Data._Semi_Auto_Fire)
{
//Mouse
if (Input.GetMouseButton(0) && !_DelayFire) { Fire_Laser(); }
if (Input.GetMouseButton(1) && !_DelayFire) { Fire_Laser(); }
//Keyoard
if (Input.GetKey(KeyCode.Space) && !_DelayFire) { Fire_Laser(); }
//Joystick
if(Input.GetKey(KeyCode.Joystick1Button0) && !_DelayFire) { Fire_Laser(); }
}
else
{
//Mouse
if (Input.GetMouseButton(0) && !_DelayFire) { Fire_Laser(); }
//Keyboard
if (Input.GetKeyDown(KeyCode.Space) && !_DelayFire) { Fire_Laser(); }
//Joystick
if(Input.GetKeyDown(KeyCode.Joystick1Button0) && !_DelayFire) { Fire_Laser(); }
}
}
#region"Phone"
public void Move_Left()
{
rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
}
public void Move_Right()
{
rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
}
public void Move_Up()
{
rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
}
public void Move_Down()
{
rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
}
public void Laser()
{
if (!_DelayFire) { Fire_Laser(); }
}
#endregion
“You can use on-screen Controls to simulate Input Devices with UI widgets that the user interacts with on the screen. The most prominent example is the use of stick and button widgets on touchscreens to emulate a joystick or gamepad.”