touch control for two keyboard buttons
Hello friends! my player is moving left and right by "a" and "d" by code add.force to object so you can avoid obstacles. I am trying to add left and right sides of mobile device's screen to act as "a" and "d" keys, I also tried gyro but it seems a bit more complicated than this. just lead me to right direction. I dont want to code.
Here is my code; public class PlayerMovement : MonoBehaviour { public Rigidbody rb; // This is reference to the Rigidbody component called "rb"
private Vector2 touchOrigin = -Vector2.one;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
// Update is called once per frame (FixedUpadate is better for Unity physics)
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime); // Adding force to z Axis (x, y, z)
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
if (Input.GetKey ("d") )
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey ("a") )
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
#else
if (Input.touchCount > 0)
{
Touch myTouch = Input.touches[0];
if(myTouch.phase == TouchPhase.Began)
{
touchOrigin = myTouch.position;
}
else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
{
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
touchOrigin.x = -1;
if (myTouch.x <= -1)
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
else if (myTouch.x >= -1)
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}
#endif
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
Answer by meozdemir90 · Jun 05, 2020 at 09:41 PM
okay i found it out, so i will put my solution here for others anyways
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb; // This is reference to the Rigidbody component called "rb"
private float ScreenWidth;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
void Start()
{
ScreenWidth = Screen.width;
}
// Update is called once per frame (FixedUpadate is better for Unity physics)
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime); // Adding force to z Axis (x, y, z)
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
if (Input.GetKey ("d") )
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey ("a") )
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
#else
int i = 0;
//loop over every touch found
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > ScreenWidth / 2)
{
//move right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetTouch(i).position.x < ScreenWidth / 2)
{
//move left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
++i;
}
#endif
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
Your answer

Follow this Question
Related Questions
Controls won't work in executable file 0 Answers
movement control for IOS 0 Answers
unity control problem 0 Answers
I cannot install two of my apps on the same device. Why? 0 Answers
Mesh colliders stop working after exporting to iOS device. 0 Answers