• 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 ToeRez1222 · Apr 09, 2012 at 12:15 AM · javascriptmovementmouseclickrts

RTS style mouse click and unit move to mouse click

I am currently trying to make an RTS style game in Unity, but I am having trouble trying to get a unit to move to where I click the mouse button.

I'm using a raycast to get the coordinates and then using LookAt() to have the unit face where the mouse click took place. My problem is I'm stumped on how to make it move towards the mouse click.

I've tried rigidbody.addforce(), transform.forward(), transform.Translate() to just make the unit move foward since it is already facing in the proper direction. Is there a function I can try to get my unit to move foward, or is there a better way of getting the unit to move towards the mouse click?

I've been playing around with a cube in a new scene and trying to write code to make it face in the direction of a mouse click and move forward towards that click, here's my code that I have already: (Please excuse me as I'm new to javascript and trying anything I can think of to make it work)

Any idea is appreciated big time!

function FixedUpdate ()

{

  if(Input.GetMouseButtonDown(0))
         ShootRay();
 }

function ShootRay()

{

  var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

  var hit: RaycastHit;

     if(Physics.Raycast(ray, hit, Mathf.Infinity))
         {
             cube = GameObject.Find("Cube");
             
                cube.transform.LookAt(Vector3(hit.point.x,0,hit.point.z));
                cube.transform.Translate(hit.point.x,0,hit.point.z);
         
         }
         
 }
Comment
Add comment · Show 1
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 jpeele · Apr 20, 2012 at 09:27 PM 0
Share

you have a good start here, you should be more specific about exactly what you're trying to do. rts generally use some sort of pathfinding, so that is a area you might want to look into, its very comlicated though. for a simple program you can start by just using physics.addForce to the forward vector, and lerp the lookAt directioin so it slowly rotates towards the destination and moves forward.Or give you cube a character controller and use controller.simple move .

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Atrius · Apr 21, 2012 at 02:50 AM

Lord Richard's answer is pretty spot on. I figured I would elaborate on it a bit to give you a working point.

Please excuse any syntax errors here as I usually work in C# but wrote this in Javascript since that appears to be what you are working in.

I created two separate scripts for this concept. One I called CommandMove.js, this is assigned to the player camera, it would essentially be in your input scripts. The goal of this script is basically what your above one is.

Goals: Receive user input, Tell unit GameObject to move.

The only changes I made were as follows:

 function ShootRay() {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
 
     if(Physics.Raycast(ray, hit, Mathf.Infinity)) {
         var moveScript : UnitMoveable = unit.GetComponent("UnitMoveable");
         if (moveScript) {
             moveScript.MoveTo(hit.point);
         }
     }
 }

In this code assume that "unit" is a GameObject var that is your unit you want to move (ie. your cube). What this does is creates a variable for a script component called UnitMoveable.js, if that component is assigned to the object, then it calls the MoveTo function on that Unit, passing in the Vector3 point that you clicked.

UnitMoveable.js is as follows:

 var moveSpeed : float = 2.0f;
 private var vDestination : Vector3;
 private var isMoving : boolean;
 
 function Start () {
     isMoving = false;
 }
 
 function FixedUpdate () {
     if (isMoving) {
         if (transform.position.Equals(vDestination)) {
             isMoving = false;
         }
         else {
             // Vector will do a linear interpolation between our current position and our destination
             var v : Vector3;
             v = Vector3.Lerp(transform.position,vDestination,Time.fixedDeltaTime * moveSpeed);
 
             // Raycast to the surface below our next destination step to make sure we are standing on it
             // this is probably not efficient for large numbers
             var hit : RaycastHit;
             if (Physics.Raycast(v, Vector3.down, hit, 5.0f)) {
                 // collider.bounds.extents.y will be half of the height of the bounding box
                 // only useful if your pivot point is in the center, if it's at the feet you don't need this
                 v.y = hit.point.y + collider.bounds.extents.y;
             }
             // set our position to the new destination
             transform.position = v;
         }
     }
 }
 
 function MoveTo (vPoint:Vector3) {
     vDestination = vPoint;
     isMoving = true;
 }


I commented in-line, but essentially what his does is stores a Vector3 for the destination, and whether the unit is moving or not (optional), plus the units movement speed.

When it gets told to MoveTo all it does is set the new destination point based on what ShootRay() sent it. Then it says it's moving. FixedUpdate then Lerps between the current point and the destination adjusting by movespeed.

I also added a little piece of code here that raycasts down from the unit to the surface below to adjust the height of the destination point so that it travels across uneven terrain. I am unsure if this is the best or most efficient way to do this, but thought I would include it.

Hope this helps you get started.

Comment
Add comment · Show 1 · 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 skitch · May 16, 2012 at 11:52 AM 0
Share

This one really helped me out!

avatar image
0

Answer by 1337GameDev · Apr 20, 2012 at 11:37 PM

What I chose to do is make a selection manager object. Which I believe you have. Then I assign it a changeCommand(int i) method to give it a command. In the move command I can have a target location or give a noted path function to move to the location based on an array list of positions (avoiding obstacles) to get to that location. I used a transform lerp command to move the unit alwaysnforward and then used look at to turn it to the destination location. Make your code modular so you can reuse it for other units and change the specific behavior. You can ignore the path nodes for now if you just need a simple turn and move operation right now though.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

What`s more expensive GUI Texures or Quads for displaying units health?(RTS) 1 Answer

RTS Grid Idea? 1 Answer

RTS movement with walking animation. 1 Answer

Enemy circles player instead of attacking. 1 Answer

Is There Such a Thing as an RTS Starter Pack? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges