Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jfulmer · Apr 29, 2012 at 12:55 PM · cameramovementrts

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?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 29, 2012 at 01:07 PM 0
Share

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.

avatar image Lo0NuhtiK · Apr 29, 2012 at 01:09 PM 0
Share

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.

avatar image fafase · Apr 29, 2012 at 01:10 PM 0
Share

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.

avatar image Lo0NuhtiK · Apr 29, 2012 at 01:11 PM 0
Share

yeah lol he said somethin about pathfinding, I just missed a bunch of what he said the first time I looked at it :D

avatar image Bluntweapon · Apr 29, 2012 at 01:14 PM 0
Share

He's not looking for pathfinding at the moment, just a teleport.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jfulmer · Apr 29, 2012 at 01:24 PM 0
Share

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.

avatar image jfulmer · Apr 30, 2012 at 06:44 AM 0
Share

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.

avatar image fafase · Apr 30, 2012 at 06:49 AM 0
Share

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.

avatar image Bluntweapon · Apr 30, 2012 at 06:55 AM 0
Share

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.

avatar image jfulmer · Apr 30, 2012 at 07:01 AM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity3d RTS style camera movement is jittery 0 Answers

3D RTS camera 3 Answers

Move Camera According to Mouse Movement While Button is Pressed 1 Answer

Fixing Issues with custom RTS Camera Controll Script 1 Answer

Disable Camera Movement,Stop camera movement 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges