Hello everyone.
I made a script here for my game, this script let me move my game object using only mouse. I can move my game object by clicking on the screen anywhere and move the mouse.
Now there problem that I;m having with this script is when I make the “sensitivity” unit to “2” it work like a charm. But when I tried to up the level on “sensitivity” let say to “5” my object will suddenly jump its position from the position it suppose to stay before I move the object.
And the other problem is, I have to set the object position to 0, 0, 0…and if I change the position of the object other that 0, 0, 0 it will jump position as well. Why is this?
using UnityEngine;
using System.Collections;
public class ControlMouse : MonoBehaviour {
public bool typeTouch;
public GameObject player1;
public float sensitivity;
public Vector3 mousePos;
public Vector3 worldPos;
public Vector3 lasPos;
public Vector3 newDelta;
public Vector3 screenPos;
private Vector2 direction;
public bool pressed;
public Camera theKamera;
private RaycastHit hit;
private Ray ray;
// Use this for initialization
void Start ()
{
typeTouch = true;
player1 = GameObject.FindWithTag ("fighterOne");
}
// Update is called once per frame
void Update ()
{
if (typeTouch)
{
MouseOn ();
}
if (pressed)
{
mousePos = new Vector3 (newDelta.x + Input.mousePosition.x, newDelta.y + Input.mousePosition.y, newDelta.z);
worldPos = theKamera.ScreenToWorldPoint (mousePos);
worldPos -= lasPos;
player1.transform.position = worldPos * sensitivity;
}
else
{
screenPos = theKamera.WorldToScreenPoint (player1.transform.position);
newDelta = new Vector3 (screenPos.x - Input.mousePosition.x , screenPos.y - Input.mousePosition.y, screenPos.z);
lasPos = worldPos;
}
}
void MouseOn ()
{
ray = theKamera.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButton (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
if (hit.collider.tag == "mouseControl")
{
pressed = true;
}
}
}
else
{
pressed = false;
}
}
}