- Home /
Moving a gameobject with mouse
Hello all.
I have just begun to play with Unity and trying to learn by doing small, simple projects and following tutorials. However - some of the (simplest) thing I want do to seems very hard by this point. Therefore I beg you to be kind - I promise I'll be better soon :)
Yesterday I begun a simple project which I need to make a gameobject move by mouse input. I have attached the following script to the gameobject (an excerpt):
if(Input.GetAxis("Mouse X") < 0) {
transform.position.x -= playerMoveSpeed * Time.deltaTime;
//print("Moved left");
}
if(Input.GetAxis("Mouse X") > 0) {
transform.position.x += playerMoveSpeed * Time.deltaTime;
//print("Moved right");
}
if(Input.GetAxis("Mouse Y") > 0) {
transform.position.z += playerMoveSpeed * Time.deltaTime;
//print("Moved up");
}
if(Input.GetAxis("Mouse Y") < 0) {
transform.position.z -= playerMoveSpeed * Time.deltaTime;
//print("Moved down");
}
This should be simple in my mind, but the gameobject don't follow the mouse! I can move the object right after starting the game, but the object is moving slower and slower and almost stop moving even with large mouse movements. The mouse pointer and objects is also not the same (the pointer start at Unitys "playbutton" above the gamescreen, but the object is on the bottom).
Can anybody give me a hint how to solve this, or tell me if it's possible at all.
Starting with small projects and working your way up is the right way to go about learning this. You'll be doing fine!
Now what you want to do is already a bit more advanced. To see an example of your approach, see the second piece of code here. But if you actually want the object to follow your mouse, it's a bit more tricky: You will need to raycast your mouse position into the game world. But is that what you want to do?
He asks how to do something and you say it's to hard for you..... really? If you can't help him that's fine. But don't write a paragraph saying it's to hard. Because his write it actually is pretty simple.
Yes, I suspect I have to raycast to do what I want. The cube should follow the mouse movement exactly, but clamped between the playfields borders. I have tried to "convert" the mouse position to screen coordinates using Camera.ScreenToWorldPoint with a hard-coded z-value but that value doesn't seem to ever change.
It seems I've got it to work - kind of :) Posting the code here:
function Update () {
var mp : Vector3 = Input.mousePosition;
mp = Vector3(mp.x,mp.y,transform.position.y+30);
var sp : Vector3 = cam.ScreenToWorldPoint(mp);
transform.position = Vector3(sp.x,0,sp.z);
if(Input.Get$$anonymous$$ouseButtonDown(0)) {
Instantiate(bullet,transform.position,transform.rotation);
}
}
Anyone answering your question needs some critical information:
Is the camera directly facing positive 'z', directly facing negative 'y', or at some angle?
Are you using an Orthographic or a Perspective camera?
Answer by JusSumGuy · Oct 10, 2018 at 06:12 AM
It is simple. I was actually having the same problem a while back. Step 1: Make a Canvas and resize it to the scope of your main camera view. Step2 : Make an Image a blank Image and set it's alpha to 0. The image should also take the same space as the Parent Canvas. you set the alpha to 0 so it's invisble. Step3: Write this script and attach it to the image object.
using UnityEngine; using UnityEngine.EventSystems; public class MousePostioning : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler { public Transform object; public void OnPointerUp(PointerEventData data) { //---- } public void OnPointerDown(PointerEventData mouse) { object.position = Camera.main.ScreenToWorldPoint(mouse.position); } public void OnDrag(PointerEventData mouse) { object.position = Camera.main.ScreenToWorldPoint(mouse.position); }
`
Then just drag and drop what ever you want to move into the object spot and your good to go. Just make sure the image and it's parent Canvas are enabled during play time.
using UnityEngine;
using UnityEngine.EventSystems;
public class $$anonymous$$ousePostioning : $$anonymous$$onoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {
public Transform object;
public void OnPointerUp(PointerEventData data) { //---- }
public void OnPointerDown(PointerEventData mouse)
{
object.position = Camera.main.ScreenToWorldPoint(mouse.position);
}
public void OnDrag(PointerEventData mouse)
{
object.position = Camera.main.ScreenToWorldPoint(mouse.position);
}
You can put the bullet instantiation in the functions too.
Your answer
Follow this Question
Related Questions
Input System Can't Catch Event on Update 0 Answers
3D Movement with Gamepad with Input System? 3 Answers
Move object towards mouse in full 3D space 2 Answers
Linux and Mouse Wheel,Linux and Mouse ScrollWheel 0 Answers
How to Move a Cube 3 Answers