- Home /
Mouse driven circular movement
I want the player to move in a circle. I did the following using cursor keys to move. I simply had the camera looking at 0,0,0 and the player at 0, -5.0, 0. Using left and right had the object moving in a circle with a radius of 5.0
var speed = 100.0;
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
transform.RotateAround(Vector3.zero, Vector3.forward, x);
}
The problem is when in the top half of the screen, left moves right, right moves left, which is counter-intuituve for many, plus it is a digital movement.
I was looking for a similar movement, but the player clicks the mouse, the code works out the angle from 0, 0, 0 and moves the player smoothly to that angle (either constant speed or accelerating, but preferably acceleration).
A secondary consideration is how to deal with multiple clicks. I'm currently thinking it moves to click1, then moves to click2, etc. The other possibility is always go towards the latest click.
I'm struggling to understand how to deal with angles, so help on any stage of this problem is most welcome.
Answer by andeeee · Feb 25, 2010 at 04:54 PM
You can get the location of a mouse click in world space by calling Camera.ScreenToWorldPoint on the current mouse position. Since the centre of the circle is at <0,0,0> this location can also be used as a direction vector.
If the player needs to stay exactly the same distance from the centre, you could simplify things by putting the player into a parent object at a set distance from the anchor point. Then, instead of using transform.RotateAround, you could just rotate the parent object to move the character around. It is straightforward to make a rotation from the clicked direction vector using Quaternion.LookRotation. You can sweep smoothly between the player's current rotation and the new rotation using Quaternion.Slerp.
Thank you for the help. I'm still learning. $$anonymous$$ost of Unity has been quite easy to understand, but something just don't click, mouse input and dealing with angles have been my biggest problems. I have re-read the sections you mentioned, but I still don't get it. This is the nearest I have got to anything working, and it is rubbish.
var speed = 100.0;
function Update () {
var relativePos = Vector3.zero - Input.mousePosition; var rotation = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
}
Your answer

Follow this Question
Related Questions
Move object towards mouse but if mouse moves object fallows it's original path 0 Answers
Checking if mouse dragged from one point to another 2 Answers
progressive circular player movement 1 Answer
Having trouble with implementing circular player movement 1 Answer
Need Help: Unity draw-to-follow? 0 Answers