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 MasonK · Jan 08, 2014 at 08:12 PM · c#positioncharactercontrollermouse

Move an object towards a point using raycast from Main Camera

I'm trying to make an RTS, so I need to be able to right-click to where I want my unit to go. So far, I've gotten it so that I can select a unit with a script in the main camera, and then click to teleport the unit to a point. I don't want the unit to teleport, but instead to move towards the point.

Here's my code for the movement:

 void Update(){
      if (Input.GetButtonDown ("Fire2") && target.tag == "Unit") {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)){
             destination = hit.point;
             target.rigidbody.MovePosition(destination);
         }
     }
 }
Comment
Add comment · Show 3
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 Gruffy · Jan 08, 2014 at 08:48 PM 0
Share

It sounds like you want to Lerp from one point to another based on your current transform`s position and the position of your mouse relative to the world.

You should read this... ScreenToWorldPoint And this... Lerp

these will help you to solve your problem. :) If you would like the script revised to resolve your problem at hand, could you post the whole script please dude. It saves time guessing stuff, for stupid ol me at least. Thanks for reading Gruffy

avatar image MasonK · Jan 08, 2014 at 09:43 PM 0
Share

I tried both, but neither worked. And thanks for offering to revise all of my code, but I'm kind of doing this to $$anonymous$$ch myself how to debug and whatnot.

avatar image Gruffy · Jan 09, 2014 at 10:51 PM 0
Share

fair enough buddy, good on ya

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MasonK · Jan 08, 2014 at 10:40 PM

Nevermind, I fixed it. First I converted the .MovePosition to .MoveTowards, but it still jumped around. It turns out that the code was only executing during the frame in which you right-clicked, so I moved it out of the if statement it was in and adjusted the code accordingly.

Here's the entire script, which is placed into the main camera. The way I set it up, you always need to have a target selected, so to prevent errors you need to put a gameObject with a rigidbody into "bTarg".

public class ClickTarget : MonoBehaviour {

private GameObject target; private Vector3 destination; private float distance; private Vector3 tTrans;

public GUIText targetDisplay; public float speed; public GameObject bTarg;

void Start () { targetDisplay.text = ""; distance = 0.0f; target = bTarg; }

void Update () { if(Input.GetButtonDown("Fire1")){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit)){ if(hit.collider != null){ if(hit.collider.tag == "Unit"){ target = hit.collider.gameObject; targetDisplay.text = "Unit: " + hit.collider.gameObject.name; destination = target.transform.position; target.rigidbody.freezeRotation = false; } if(hit.collider.tag == "Building"){ target = hit.collider.gameObject; targetDisplay.text = "Building: " + hit.collider.gameObject.name; } } } } }

void FixedUpdate(){ if (Input.GetButtonDown ("Fire2") && target.tag == "Unit" && GUIUtility.hotControl == 0) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray,out hit)){ destination = hit.point; } }

 tTrans = target.transform.position;
 distance = Vector3.Distance (tTrans, destination);

 if(target.tag == "Unit"){
     if (distance > .2f) {
         target.transform.LookAt (destination);
         target.transform.position = Vector3.MoveTowards (target.transform.position, destination, speed);
         target.rigidbody.freezeRotation = true;
     }
 }

} }

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 sath · Jan 09, 2014 at 06:37 AM 0
Share

tick your answer as correct to help others from community and please post your working script too

avatar image
0

Answer by sath · Jan 08, 2014 at 09:14 PM

attach this script to a gameobject and it will follow mouse hit position

do the changes you need to make it work with your project

     var speed = 1;
     var targetRadius = 1;
     private var target : Vector3;
      
     function Start () {
     //    rigidbody.freezeRotation = true;
         target = transform.position;
     }
      
     function FixedUpdate () {
         target.y = transform.position.y;
         if (Input.GetButtonDown ("Fire1")) {
             var hit : RaycastHit; 
             var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray, hit)) {
                 target = hit.point;         
             }
         }   
      
         if (Vector3.Distance(transform.position, target) > targetRadius) {
                 transform.LookAt(target); 
                 transform.Translate(0,0,speed);                 
         }
     }
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 MasonK · Jan 08, 2014 at 09:45 PM 0
Share

I need my code to be in my main camera so I don't have to put it into each an every unit, and so that I can select different units. I tried to modify this to fit into my camera, but the unit still just teleports around, which is what I was trying to fix in the first place.

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

19 People are following this question.

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

Related Questions

OnMouseEnter/Exit problem 2 Answers

Positioning a GameObject so I't wont be inside another object 1 Answer

how to make my character go round loops? 1 Answer

error CS8025: Parsing error 1 Answer

CharacterController in instance object 1 Answer


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