- Home /
Click-to-move questions
Hey all hope some of you might be able to solve these problems I'm having with Click-to-move movement.
My character is going through walls, and using a rigidbody makes it rotate in all sorts of directions when it moves(though it does stop the character from going through the walls), to make it not rotate at all I have to freeze all of the rotations on the rigidbody but by doing this the character goes through walls again. What can I do to fix this? script - http://unifycommunity.com/wiki/index.php?title=Click_To_Move
I want to do a Runescape or League of Legends type path system where when the player's location and the clicked location have obstacles, a path is drawn to take the character around the obstacles. I have absolutely no idea how to go about doing this.
Thanks to all willing to help me!
Answer by aldonaletto · May 25, 2012 at 12:56 AM
The script ClickToMove is intended to move objects without checking collisions. If you add a rigidbody to the object, it will react to collisions and spin, bounce etc. A better solution could be to add a CharacterController to the character - but the script should be modified to use SimpleMove, or the collisions would not be checked:
var speed: float = 5; // Determines how quickly object moves towards position
private var targetPosition:Vector3; // current target pos private var character: CharacterController;
function Start(){ character = GetComponent(CharacterController); targetPosition = transform.position; }
function Update () { if(Input.GetKeyDown(KeyCode.Mouse0)){ // create a logical horizontal plane at the player position: var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0; // find point clicked in the plane (if any): if (playerPlane.Raycast (ray, hitdist)) { // update targetPosition to the clicked point: targetPosition = ray.GetPoint(hitdist); var dir = targetPosition - transform.position; dir.y = 0; // keep only the horizontal direction transform.rotation = Quaternion.LookRotation(dir); } } // always try to move the character to targetPosition: character.SimpleMove(dir * speed); // move taking gravity into account } This script makes the character continuously try to reach the point defined by the variable targetPosition; when you click anywhere in the scene, it creates a temporary logical horizontal plane at the character position and finds the clicked point, if any, and assign it to targetPosition, thus the character moves to this new position. If no valid point is clicked (when you click the sky, for instance) targetPosition isn't modified, thus the character doesn't change direction.
NOTE: You must remove the rigidbody from your character and add a CharacterController to it (menu Component/Physics/Character Controller).
NOTE 2: Your second question is way more complicated: you should use some pathfinding script, like AStar or the built in Unity pathfinding resource (Pro required).
Answer by rubenpvargas · May 24, 2012 at 11:49 PM
first.- Sorry for bad english
second.- im not sure if this is the best solution
Maybe you can use a linecast (a ray that go to point A to point B) to check obstacles in the way (from your caracther to the destination). when you have detected the obstacles, you can add a "navigation point" or something like that.
For example:
If linecast hit "wall"
then move to "wallNavigationPoint"
then move to final destination
Bassed on this, you can make your code more and more complex to create a very nice movement
This is very interesting, I'm going to have to give this a try!