- Home /
Error with moving object to mouse clicked coordinates. [SOLVED]
I made a little script that should take a coordinates an the move object to it. But is moves object to an opposite direction. I am workin with 2d scene.
Here is the code:
public Camera MainCamera; public LayerMask layers; public float movementSpeed = 20.0f; private bool IsMoving = false; private Vector3 targetPosition;
// Use this for initialization
void Start()
{
targetPosition = transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(1))
{
SetCoordinates();
}
if (IsMoving)
{
Moving();
}
}
private void SetCoordinates()
{
Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layers))
{
targetPosition.x = hit.point.x;
targetPosition.y = hit.point.y;
targetPosition.z = transform.position.z;
Debug.DrawRay(transform.position, targetPosition, Color.red);
}
IsMoving = true;
}
private void Moving()
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
if (transform.position == targetPosition)
{
Debug.Log(targetPosition);
Debug.Log(Input.mousePosition + " mouse pos");
IsMoving = false;
}
}
}
Answer by LiloE · Mar 05, 2017 at 11:01 AM
Use ScreenToWorldPoint instead of Raycast
In 2D scenes like yours, use the Camera.main.ScrenToWorldPoint
function instead of rays.
The Raycast is for 3d physics.
If you need to detect a hit on a game object, use Physics2D.OverlapPoint
Basic Example
var position = Camera.main.ScreenToWorldPoint(mousePosition);
var col = Physics2D.OverlapPoint(position);
Example for moving a target to mouse location
public class MovePointToPoint : MonoBehaviour {
public float Speed = 5;
Vector3 target = Vector3.zero;
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = 1;
}
if (transform.position != target) {
float step = Speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, step);
}
}
}
I changed code like this:
private void SetCoordinates()
{
targetPosition.x = $$anonymous$$ainCamera.ScreenToWorldPoint(Input.mousePosition).x;
targetPosition.y = $$anonymous$$ainCamera.ScreenToWorldPoint(Input.mousePosition).y;
targetPosition.z = transform.position.z;
Is$$anonymous$$oving = true;
}
private void $$anonymous$$ove()
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
if (transform.position == targetPosition)
{
Is$$anonymous$$oving = false;
}
}
And object start to move just on Y coordinates.
I also used a hit detection but object moved far away from mouse coordinates. Something like this mouse in x=10, y=10, and the object moves above it on x=100, y=100.
$$anonymous$$y mistake I guess. Just fixed a few thing an all working good thank you.
private void SetCoordinates()
{
targetPosition.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
targetPosition.y = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
targetPosition.z = transform.position.z;
Debug.Log(targetPosition);
Is$$anonymous$$oving = true;
}
private void $$anonymous$$ove()
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
if (transform.position == targetPosition)
{
Is$$anonymous$$oving = false;
}
}
Great, but see my example for a more streamlined option. You can convert the entire vector once and just modify the z component later.