x


Javascript - Mouse-Click movement

I am trying to figure out how to do the mouse-click movement effect in my project. Right now my character (3rd person view) works just fine using the w,a,s,d controls. But instead of those controls, I want the player to be able to point where he/she wants to go, and then clicks and the player will automatically go to that spot.

Similar to Archlord, Wow, etc...(at least I think WoW has that type of movement)

Can anyone point me in the right direction or help me out with this? Thanks in advance...

more ▼

asked Dec 19 '09 at 08:00 PM

SpiritWebb gravatar image

SpiritWebb
136 6 8 20

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

2 answers: sort voted first

Sounds very much like you need to look at "Pathfinding" solutions.

It's relatively trivial to get the world position on your terrain which corresponds to where the user clicked the mouse - see the Collider.Raycast docs for more on this - that page actually gives the following example using mouse clicks:

// pragma below is needed due to a UnityJS issue
#pragma strict

function Update(){
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (collider.Raycast (ray, hit, 100.0)) {
        Debug.DrawLine (ray.origin, hit.point);
    }
}

The more complex part is what to do once you have this destination point. You will rarely want the character to move in a direct line straight there because you'll have buildings, trees, walls, etc in your way. This is where pathfinding algorithms come into play.

This has already been covered on Unity Answers, so for more information about pathfinding solutions for Unity, check this link which will give you useful answers from this site tagged with pathfinding:

http://answers.unity3d.com/questions/tagged/pathfinding

more ▼

answered Dec 19 '09 at 09:42 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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

answered Dec 20 '09 at 01:42 PM

xaddict gravatar image

xaddict
142 7 8 19

(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:

x985

asked: Dec 19 '09 at 08:00 PM

Seen: 7915 times

Last Updated: Dec 19 '09 at 08:00 PM