Hello! Decided to give Unity a go - and I've really enjoyed it so far. Now, I'm stuck. I'm trying to make an object follow the mouse cursor when the left mouse button is pressed down (this is in 2D space). I've dug up a few threads on this topic, but I still can't quite puzzle it together. I'd also like to add acceleration and retardation, as well as inertia, to the object in question. I've taken a gander at the script reference, but me being totally green it's not making too much sense. I'd really appreciate someone shedding some light on this.
I'm by no means a pro...but when I was rigging up a small tech demo of what Unity can do... I managed to rig up a flying spaceship that would orient itself towards where I clicked on the screen in 3d space. This was the key line.... Code (csharp): transform.LookAt(Camera.main.ScreenPointToRay (Input.mousePosition).GetPoint (1000)); Essentially this made the ship "look" at the point in space where I clicked. That way in the update method I could use the transform.forward direction to move. The ScreenPointToRay method was the key to accomplishing this as it can take where you click and convert it to a world space co-ordinate. (the get point was just so I could make it a farther distance so that the difference between my camera (which would orbit around the object) and the ship would not be very noticable. I don't know if it was required to be that long..it was just a quick and dirty test. Hope this somewhat helps. Cheers
Would you mind posting a full example? I'm not sure how to set up transform.Forward properly. Cheers!
Hi my first post on this forum. Thanks to all people that giving great feed back and helping out. Maybe this Followmouse2d is to some help.
Nice i use it too thanks but i was thinking how to let it follow everytime so you dont have to click before it cdomes to there
Hey Wox, very nice script... I've been trying to make this work with raycasting for like 1/2 hours, but you solved it for me!!!
@StickyNavels, At the moment, the script doesn't have any speed, it just goes right to the spot you clicked on... I'm not a pro, but I think you could implement speed by getting the distance between your player and the spot, normalize that distance (distance.normalized) and make the player Translate along that e.g. ----- Code (csharp): var hit_x = hit.point.x; var hit_z = hit.point.z; moveDirection = Vector3(hit_x, 0, hit_z) - transform.position; transform.Translate(moveDirection.normalized*speed); ------- I hope that helps you out, Dalionzo