- Home /
GameObject follow mouse
So I'm trying to have a game object follow the mouse position but i was using out dated tutorials and guides so i had to come up with my own code. Sadly i'm kinda stuck because it tells me direction isn't part of unity vector 3? ray is the original code, but ray won't follow mouse positions, mouseworldposition works fine though.
#pragma strict var ball:GameObject; function Start () {
}
private var hits:RaycastHit[];
var hit:RaycastHit;
private var ray:Ray;
function Update () {
var mouseWorldPosition : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
ray=Camera.main.ScreenPointToRay(Input.mousePosition);
//Debug.DrawLine(transform.position, mouseWorldPosition, Color.red);
hits=Physics.RaycastAll(Camera.main.transform.position,ray.direction,50);
for (var i = 0;i < hits.Length; i++){
hit = hits;
if(hit.collider.tag=="screen")
{
ball.transform.position=hit.point;
}
}
}
Answer by Torigas · Nov 24, 2015 at 04:51 PM
Here's what i hacked up yesterday. This works if the Object has to move on an X-Y Plane at a certain distance to the camera.
using UnityEngine;
using System.Collections;
public class MoveWithMouse : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 newPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Mathf.Abs(Camera.main.transform.position.z-transform.position.z)));
newPos.z = transform.position.z;
transform.position = newPos;
}
}
This also sets the position. If you want a smooth follow you can do something like
transform.position = Mathf.lerp(transform.position,newPos,0.5f);
If you want this to work on arbitrary scenes it gets slightly more complicated and you have to do an actual raycast like you tried. However this will fail if the object you move has a collider, since the system will detect a collision with the object you move and thus keep moving the object towards you:
using UnityEngine;
using System.Collections;
public class MoveWithMousePhysical : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Vector3 newPos = hit.point;
transform.position = newPos;
}
}
}
Now to properly stick to the first object it touches you can either work with layer masks so the raycast does not collide with the object it moves or you can take all the collisions and iterate over them:
using UnityEngine;
using System.Collections;
using System.Linq;
public class MoveWithMousePhysical : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
RaycastHit[] hitpoints = Physics.RaycastAll(ray).OrderBy(h => h.distance).ToArray(); //sort by distance
foreach (RaycastHit hit in hitpoints)
{
if(hit.collider.gameObject != this.gameObject)
{
Vector3 newPos = hit.point;
transform.position = newPos;
break; //stop iterating
}
}
}
}
wow, thanks for the rely, having a bit of problem incorporating your code, the sphere or ball var is a collider. I'm actually following a pretty old tutorial around the 14 $$anonymous$$ mark is where this code is giving me problems. I've been replacing code that's no longer working with functioning code YouTube video
Your answer
![](https://koobas.hobune.stream/wayback/20220612050136im_/https://answers.unity.com/themes/thub/images/avi.jpg)