Hello there…
I have make this script for my game. This script I made is to be used on mobile devices. But I need to change this script so I can used is on pc with mouse as well. So I’m am getting a bit of trouble with changing the script so it can used mouse input. What should I do to change the script so it will work with mouse.
using UnityEngine;
using System.Collections;
public class ControlMouse : MonoBehaviour {
public GameObject player1;
public bool typeTouch;
public float moveSpeed;
private float moveX;
private float moveY;
private Vector3 move;
public Touch touch;
public Camera theKamera;
private RaycastHit hit;
private Ray ray;
public HoverManager hover;
private Vector2 direction;
// Use this for initialization
void Start ()
{
typeTouch = false;
player1 = GameObject.FindWithTag ("fighterOne");
ScreenLock ();
}
// Update is called once per frame
void Update ()
{
if (typeTouch)
{
MouseOn ();
}
}
void MouseOn ()
{
ray = theKamera.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButton (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
if (hit.collider.tag == "player1Control" && Input.touchCount == 1)
{
touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Moved)
{
moveX = moveSpeed * touch.deltaPosition.x;
moveY = moveSpeed * touch.deltaPosition.y;
move = new Vector3 (moveX, moveY, 0.0f);
player1.transform.position += move * Time.deltaTime;
}
if (touch.deltaPosition.x > 0)
{
hover.front = true;
}
else if (touch.deltaPosition.x < 0)
{
hover.back = true;
}
else
{
hover.front = false;
hover.back = false;
}
if (touch.deltaPosition.y > 0)
{
hover.up = true;
}
else if (touch.deltaPosition.y < 0)
{
hover.down = true;
}
else
{
hover.up = false;
hover.down = false;
}
}
}
}
}
void ScreenLock ()
{
direction = player1.transform.position;
if (direction.x >= 7f)
{
direction.x = 7f;
player1.transform.position = direction;
}
else if ( direction.x <= -7)
{
direction.x = -7f;
player1.transform.position = direction;
}
if (direction.y >= 5.7f)
{
direction.y = 5.7f;
player1.transform.position = direction;
}
else if (direction.y <= -5.0f)
{
direction.y = -5.0f;
player1.transform.position = direction;
}
}
}
Thank you.