- Home /
RTS Mouse Click Movement
Hello,
Currently I have the ability to select a unit on the map. I'm very new to unity scripting, so before I implemented path finding I wanted to see if I could just automatically move the selected object to a point on the map that I click on. The code that I have come up with through some research:
var mousePo = Camera.main.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(mousePo);
if(Physics.Raycast(ray.origin,ray.direction, hit))
{
transform.position = mousePo;
}
The camera I am using is above the terrain with an X rotation of 40 looking down onto the terrain from an angle (as is common in most RTS from my experience.) The problem seems to be that it is spawning the object at my camera location and dropping it onto the map below the camera, which is obviously not the desired effect. Could someone please point me in the right direction on how I can accomplish this?
Your main problem is that you are not moving the obejct there, you are simply giving it a new position. You teleport it ins$$anonymous$$d of moving it.
I thought they wanted to teleport it? Like, instantiating, but with an object already out there somewhere hiding lol...idk been up all night again.
EDIT never$$anonymous$$d lol I just re-read the question.
I based my answer on "The problem seems to be that it is spawning the object at my camera location and dropping it onto the map below the camera" so I reckon he wants something to walk there like a strategic game style where you tell to move somewhere and they walk there. I could be wrong.
yeah lol he said somethin about pathfinding, I just missed a bunch of what he said the first time I looked at it :D
He's not looking for pathfinding at the moment, just a teleport.
Answer by fafase · Apr 29, 2012 at 01:06 PM
1: Get the position of the mouse click and make it a target start the going .
2: Check if the target is close to the object.
3: Make the object look at the target with transform.LookAt(target);
4: Get the object moving there with transform.Translate(0, 0, Time.deltaTime);
5: Distance is close enough, we close the moving
var target:Transform;
var going:boolean;
function Start(){
target.position =transform.position;
going =false;}
function Update(){
if(Input.GetMouseButtonDown(0)){ // the button is pressed
target.position=Input.mousePosition;
going = true;}
if(going){
if((Vector3.Distance(target.position,transform.position))>0.5){
transform.LookAt(target);
transform.Translate(0, 0, Time.deltaTime);
}
else
going =false;
}
EDIT: I added a Start function to resolve your problem. Also, I added an if statement that would solve some later problems (5:) Sorry I have been reediting as I found mistakes on my scripts...
For the distance I put 0.5 because if you compare with 0 it might never really reach it and your object would keep on trying moving around the target without stopping on it.
So I put this in to my javascript, had to change some things, such as:
target.position = Input.mousePosition;
I'm getting an error that target hasn't been assigned for this reason. I'll figure out how to get this working in Javascript and let you know if it works.
I'm not sure how to fix the errors I am getting from following this direction. It seems like the target needs to be an actual object in order to look at it.
target = transform.position; does not work because target is Transform and position is Vector3
this is the same problem for
target = Input.Get$$anonymous$$ousePosition and placing target in the Distance function.
I tried just changing all these uses to target.position, however when I run the game I get an error that target is unassigned. Which I expected.
Do I need to create an object where the mouse is clicked and set the target to it I wonder?
EDIT: Okay, what I did was set target = transform in the start function. This removed the errors, however it is now sending the object way off in the positive X and Y axis.
Yes, Transform is the component that includes position, scale and rotation so you need to indicate which one is to be used, I guess I was a little sleepy when I did that. At least, you are learning.
The code above makes no sense.
You can just make "target" a Vector3. The Transform.LookAt() function can take Vector3 as well as a Transform.
But beyond that, setting target.position to Input.mousePosition does absolutely nothing. Input.mousePosition is essentially a 2D vector of the mouse location in Screen Coordinates (the z is always 0). This function just makes your object look at completely arbitrary points in your worldspace.
Thanks for all your help. Right now I am doing the follows:
var mousePo = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
var hit: RaycastHit; var ray = Camera.main.ScreenPointToRay(mousePo); if (Physics.Raycast(ray.origin, ray.direction, hit)) { print("world point on terrain: " + hit.point + ", distance to point: " + hit.distance); }
So I can record where the mouse is on the terrain. However I noticed that it doesn't seem to change a whole lot regardless of where I click. Does this have something to do with the fact that my camera is initially angled at 40 degrees towards the terrain?