x


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.

more ▼

asked Feb 25 '10 at 04:25 PM

Andy 1 gravatar image

Andy 1
26 3 3 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Feb 25 '10 at 04:54 PM

andeeee gravatar image

andeeee ♦
1.4k 3 6 18

Thank you for the help. I'm still learning. Most 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);

}

Mar 02 '10 at 05:27 PM Andy 1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1001
x245
x24

asked: Feb 25 '10 at 04:25 PM

Seen: 1552 times

Last Updated: Feb 25 '10 at 04:25 PM