- Home /
Having problems with touch input
What I would like to do is set up a rotation of my 2d players arm and make him point at where your touch position on a fire button that takes up half the screen is located but instead I have done this if (CrossPlatformInputManager.GetButton("Fire1")) { if(CrossPlatformInputManager.GetAxis("Horizontal") <= 0.1 && CrossPlatformInputManager.GetAxis("Vertical") <= 0.1) { var mousePos = Input.GetTouch(0).position; // mousePos.z = 10; Vector3 difference = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane)) - transform.position; difference.Normalize(); float CurRotZ = transform.rotation.z; float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Slerp(Quaternion.Euler(0f, 0f, CurRotZ), Quaternion.Euler(0f, 0f, rotZ + rotationOffset), Time.time * 0.5f); }else { var mousePos = Input.GetTouch(1).position; // mousePos.z = 10; Vector3 difference = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane)) - transform.position; difference.Normalize(); float CurRotZ = transform.rotation.z; float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Slerp(Quaternion.Euler(0f, 0f, CurRotZ), Quaternion.Euler(0f, 0f, rotZ + rotationOffset), Time.time * 0.5f); }
Which for now works but when the player is in flight or moving, the arm when it is pointing at the touch position is super jumpy. How would I fix this? Also the reason its called mouse pos is because i was using mouse position then moved over to mobile and kept the same naming conventions.
Answer by majordillow · Jun 23, 2017 at 09:18 PM
Wow that didn't keep the formating i was hoping it would
if (CrossPlatformInput$$anonymous$$anager.GetButton("Fire1"))
{
if(CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal") <= 0.1 && CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical") <= 0.1)
{
var mousePos = Input.GetTouch(0).position;
// mousePos.z = 10;
Vector3 difference = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane)) - transform.position;
difference.Normalize();
float CurRotZ = transform.rotation.z;
float rotZ = $$anonymous$$athf.Atan2(difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Slerp(Quaternion.Euler(0f, 0f, CurRotZ), Quaternion.Euler(0f, 0f, rotZ + rotationOffset), Time.time * 0.5f);
}else
{
var mousePos = Input.GetTouch(1).position;
// mousePos.z = 10;
Vector3 difference = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane)) - transform.position;
difference.Normalize();
float CurRotZ = transform.rotation.z;
float rotZ = $$anonymous$$athf.Atan2(difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Slerp(Quaternion.Euler(0f, 0f, CurRotZ), Quaternion.Euler(0f, 0f, rotZ + rotationOffset), Time.time * 0.5f);
}
Your answer

Follow this Question
Related Questions
Sensitivity Slider Issue 1 Answer
Count a touch in half of screen 2 Answers
EventSystem.current.IsPointerOverGameObject(t.fingerId) - Not working on Android builds - Unity 2 Answers
Multitouch problem 0 Answers