Im trying to make my character move using mouse…moving the character using mouse is done, below is my code:
using UnityEngine;
using System.Collections;
public class ControlMouse : MonoBehaviour {
public bool typeTouch;
public GameObject player1;
public float speed;
private float moveXY;
public float sensitivity;
private Vector3 mouseControl;
public Camera theKamera;
private RaycastHit hit;
private Ray ray;
// Use this for initialization
void Start ()
{
typeTouch = true;
moveXY = speed;
player1 = GameObject.FindWithTag ("fighterOne");
}
// Update is called once per frame
void Update ()
{
if (typeTouch)
{
MouseOn ();
}
print (mouseControl);
}
void MouseOn ()
{
ray = theKamera.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButton (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
if (hit.collider.tag == "mouseControl")
{
mouseControl = theKamera.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10));
player1.transform.position = (mouseControl * moveXY) / sensitivity;
}
}
}
}
}
But this code makes the objects always under the mouse, what I need is the object to move relative to the mouse. In otherword instead of the object matching its coordinants to the mouse coordinates, it needs to match its movement to the mouse movement.
instead setting the playerposition where your mouse points calculate the difference vector (player.transform.position - mouseControl).normalized; for the direction.
@exigus, sorry I dont want to move my object toward my mouse, what I want to do is move the object by me click any point of the screen and move my object without my object jump to my mouse cursor.
Can you understand? kindda hard for me to explain actually.
so when the object is 100 pixels above the mouse and you move the mouse left then it should still be 100 pixels above the mouse?
if so you also need to incorporate the relative vector. so when you “pick” the object calculate the relative vector like diffvect = obj.position - mouseControl and store it in your script (not locally in the function).
then every frame determine the point where the mouse points to and add the vector to it like obj.position = mouseControl + diffvect;
this should move your object “parallel” to your mouse.
yes, thats what I want to do. is the “diffvect” = vector3.zero? What do you mean by “not locally in the function”?
So this is what I got :
using UnityEngine;
using System.Collections;
public class ControlMouse : MonoBehaviour {
public bool typeTouch;
public GameObject player1;
public float speed;
private float moveXY;
public float sensitivity;
private Vector3 mouseControl;
public Vector3 delta;
public Camera theKamera;
private RaycastHit hit;
private Ray ray;
// Use this for initialization
void Start ()
{
typeTouch = true;
moveXY = speed;
player1 = GameObject.FindWithTag ("fighterOne");
}
// Update is called once per frame
void Update ()
{
if (typeTouch)
{
MouseOn ();
}
print (mouseControl);
}
void MouseOn ()
{
ray = theKamera.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButton (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
if (hit.collider.tag == "mouseControl")
{
mouseControl = theKamera.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10));
delta = player1.transform.position - mouseControl;
player1.transform.position = mouseControl + delta;
}
}
}
}
}
but nothing happen, the game object is still under the mouse cursor. Am I doing something wrong here?
you can’t use a ray and check for the correct layer when you dont want your mouse above the object. here is an reduced example on what i meant (untested!). this should give you a start.
using UnityEngine;
using System.Collections;
public class ControlMouse : MonoBehaviour
{
public GameObject player1;
public Vector3 delta; // the vector of your object relative to camera
private bool picked;
void Start ()
{
player1 = GameObject.FindWithTag ("fighterOne"); // check here if it is found!
}
void Update ()
{
if (Input.GetMouseButton (0))
{// when user presses left mouse "pick" the object
Vector3 screenPos = camera.WorldToScreenPoint(player1.transform.position); // get where it is on the screen
delta = new Vector3(screenPos.x - Input.mousePosition.x, screenPos.y - Input.mousePosition.y, screenPos.z); // z is the distance from the camera
picked = true;
}
if (Input.GetMouseButtonDown(1))
{// "release" the object from the mouse leaving it at its position
picked = false;
}
if (picked)
{// keep the object relative to the mouse
Vector3 scrrelpos = new Vector3(Input.mousePosition.x+delta.x, Input.mousePosition.y+delta.y, delta.z);
Vector3 wrldrelpos = Camera.main.ScreenToWorldPoint(scrrelpos);
player1.transform.position = wrldrelpos;
}
}
}
Thanks for the info, manage to do the parallel movement but something wrong with my code I think, tried to add sensitivty for my object, the first time you move the object it work fine, but the second time you click the mouse it will add value to you object and jump the object from normal position to other position, below here is my script:
using UnityEngine;
using System.Collections;
public class ControlMouse : MonoBehaviour {
public bool typeTouch;
public GameObject player1;
public float sensitivity;
private Vector3 mousePos;
private Vector3 worldPos;
private Vector3 newDelta;
private Vector3 screenPos;
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 (Input.mousePosition.x + newDelta.x, Input.mousePosition.y + newDelta.y, newDelta.z);
worldPos = theKamera.ScreenToWorldPoint (mousePos);
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);
}
}
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;
}
}
}