I’m trying to move and object on the oposite direction of my mouse on a 2D game.
But I’m having problems and its not moving to the right direction.
I checked my script and dont found the error.
I think is a logical problem
Please can someone help ? I tried everything
using UnityEngine;
using System.Collections;
public class MoveBall : MonoBehaviour
{
public Transform sprite;
public Transform mousePos;
public Vector3 heading;
float distance;
Vector3 direction;
float dirX;
float dirY;
void Update()
{
heading = sprite.position - mousePos.position;
distance = heading.magnitude;
direction = heading/distance;
heading.z =0;
dirX = direction.x;
dirY = direction.y;
sprite.transform.Translate(new Vector3(dirX*Time.deltaTime, dirY*Time.deltaTime, 0));
}
}
PGJ1
June 22, 2015, 5:12pm
2
Isn’t the problem that mousePos is in pixel coordinates, with origio being at the lower left corner, whereas sprite.position is in world coordinates?
I tryied this changes but dont worked, no Idea whats wrong
using UnityEngine;
using System.Collections;
public class MoveBall : MonoBehaviour
{
public Transform sprite;
public Transform mousePos;
public Vector3 heading;
float distance;
Vector3 direction;
float dirX;
float dirY;
float mouseX;
float mouseY;
public Vector3 mousePosition;
void Update()
{
mouseX = (Input.mousePosition.x);
mouseY = (Input.mousePosition.y);
mousePosition = Camera.main.ScreenToWorldPoint(new Vector3 (mouseX,mouseY,0));
heading = mousePosition - sprite.position;
distance = heading.magnitude;
direction = heading/distance;
dirX = direction.x;
dirY = direction.y;
sprite.transform.Translate(new Vector3(dirX*Time.deltaTime, dirY*Time.deltaTime, 0));
}
}
PGJ1
June 22, 2015, 7:51pm
4
Try this:
using UnityEngine;
using System.Collections;
public class MoveBall : MonoBehaviour
{
public Transform sprite;
Vector2 lastMousePosition;
public Vector2 mousePosition;
void Start()
{
lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void Update()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mouseDelta = lastMousePosition - mousePosition;
lastMousePosition = mousePosition;
sprite.transform.Translate(mouseDelta);
}
}
1 Like
I tried but nothing happens, I printed mouseDelta and its position is (0,0)
The camera is static on screen.
On the code you sent the ball only move if I move te camera position (weird)
The Idea of the game is like a ball that I blow and it move smothly, I’m using translate but I dont know if its the best choice
PGJ1
June 22, 2015, 9:38pm
6
Damn, I was using an ortographic camera! You need to add the camera depth also. Try this (I’ve hardcoded the camera depth to 10, you probably want to change that to something appropriate for your scene):
using UnityEngine;
using System.Collections;
public class MoveBall : MonoBehaviour
{
public Transform sprite;
Vector3 lastMousePosition;
public Vector3 mousePosition;
void Start()
{
Vector3 mp = Input.mousePosition;
mp.z = 10;
lastMousePosition = Camera.main.ScreenToWorldPoint(mp);
}
void Update()
{
Vector3 mp = Input.mousePosition;
mp.z = 10;
mousePosition = Camera.main.ScreenToWorldPoint(mp);
Vector3 mouseDelta = lastMousePosition - mousePosition;
lastMousePosition = mousePosition;
sprite.transform.Translate(mouseDelta);
}
}
Thank you it worked perceftly =) I’m just having a little problem to use transform.translate now, because if a drag the mouse it go to wrong direction
I tried to transform.translate(mousePosition) but it not even move to the right direction
PGJ1
June 24, 2015, 2:34pm
8
Didn’t you say in the original post that it should go in the opposite direction? Try changing line 28 to this instead:
Vector3 mouseDelta = mousePosition - lastMousePosition;
1 Like
I think my sphere dont moving right, I tried this changes already
I’m posting here a web demo
http://dev.antidotodesign.com.br/projetos/TesteFolder/Test.html
using UnityEngine;
using System.Collections;
public class MoveBall : MonoBehaviour
{
public Transform sprite;
Vector2 lastMousePosition;
public Vector2 mousePosition;
Vector2 dir = Vector2.zero;
void Start()
{
lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void Update()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mouseDelta = mousePosition - lastMousePosition;
lastMousePosition = mousePosition;
if(Input.GetMouseButton(0) && MouseIn.mouseOver == true)
{
sprite.transform.Translate(mousePosition * Time.deltaTime * -1);
}
}
}
PGJ1
June 24, 2015, 7:11pm
10
Hmmm, dunno why I was so boneheaded, I realized that one can do this much simpler. GetAxis is already a delta, which makes everything much easier…
using UnityEngine;
using System.Collections;
public class MoveBall : MonoBehaviour
{
public Transform sprite;
void Update()
{
Vector2 mouseDelta = new Vector2(-Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
if (Input.GetMouseButton(0)) // && MouseIn.mouseOver == true)
{
sprite.transform.Translate(mouseDelta);
}
}
}
2 Likes