Turning Mouse Input into Touch
Hi, So I have this script: using UnityEngine; using System.Collections;
public class MouseLook : MonoBehaviour {
public int fireRate;
public float lookSensitivity = 5f;
public float xRotation;
public float yRotation;
public Rigidbody bullet;
public Transform barrelEnd1;
public Transform barrelEnd2;
public Transform barrelEnd3;
public Transform barrelEnd4;
void Start () {
}
void Update() {
xRotation += Input.GetAxis ("Mouse Y") * lookSensitivity;
yRotation -= Input.GetAxis ("Mouse X") * lookSensitivity;
xRotation = Mathf.Clamp (xRotation, 0, 0);
transform.rotation = Quaternion.Euler (xRotation, yRotation, 0);
if (Input.GetMouseButton (0)) {
Rigidbody bulletInstance;
bulletInstance = Instantiate (bullet, barrelEnd1.position, barrelEnd1.rotation) as Rigidbody;
bulletInstance.AddForce (barrelEnd1.forward * fireRate);
bulletInstance = Instantiate (bullet, barrelEnd2.position, barrelEnd2.rotation) as Rigidbody;
bulletInstance.AddForce (barrelEnd2.forward * fireRate);
bulletInstance = Instantiate (bullet, barrelEnd3.position, barrelEnd3.rotation) as Rigidbody;
bulletInstance.AddForce (barrelEnd3.forward * fireRate);
bulletInstance = Instantiate (bullet, barrelEnd4.position, barrelEnd4.rotation) as Rigidbody;
bulletInstance.AddForce (barrelEnd4.forward * fireRate);
}
}
}
I would like to make it so that instead of using the mouse position to rotate the player, I want to be able to drag my finger of the screen instead.
Any ideas? @Mmmpies Thanks,
Not got much time to comment at the moment but a quick search on YouTube gives me this...
https://www.youtube.com/watch?v=gGcPQt-oI-E
I think that uses touch for mobile phones, you might want to search for one that uses the 4.6 UI as it'll be a lot easier to implement.
If there is any chance you can change my script when you have the time? It would be so amazing if you could, but only when you have the time of course! @$$anonymous$$mmpies
Answer by Mmmpies · Jun 13, 2016 at 08:04 PM
First off do this tutorial https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows
You need to start understanding the EventSystem and start geting your hands dirty with some code. That said it's fairly straight forward to convert as you script simply appears to rotate the player. I got rid of all the other stuff for testing and if using the UI like this this script will need to be on a panel (which is why I added the player public variable. If you want a fire button you need to add a button to your canvas and trigger it with an OnClick.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class PointLook : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler {
public GameObject MyPlayer;
private float xRotation;
private float yRotation;
private bool pointing = false;
private Vector2 fingerMouse;
private float rotationSpeed = 5f;
void Update() {
if (pointing) {
yRotation -= (fingerMouse.x / (Screen.width / 2)) * rotationSpeed;
MyPlayer.transform.rotation = Quaternion.Euler (0, yRotation, 0);
}
}
public void OnDrag(PointerEventData ped)
{
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform> (),
ped.position, ped.pressEventCamera, out fingerMouse))
return;
}
public void OnPointerDown(PointerEventData ped)
{
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform> (),
ped.position, ped.pressEventCamera, out fingerMouse))
return;
pointing = true;
}
public void OnPointerUp(PointerEventData ped)
{
pointing = false;
}
}
I only did this because it interested me though, you really can't expect people to write scripts for you, not even me.