• 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
1
Question by Mac · Sep 30, 2011 at 12:09 AM · moveclick

C# Click to Move problem?

Hi folks. I have converted the helpful JS click to move script (http://www.unifycommunity.com/wiki/index.php?title=Click_To_Move) to C# for my project.

There are 2 problems: 1) I don't entirely understand how it works; 2) It is not working as advertised. Let's tackle Problem #2 first! I'll paste in the code I have on my Player.cs script so far:

 public class Player : MonoBehaviour {
 
 public float PlayerMoveSpeed;
 private Vector3 TargetPosition;
 private Transform PlayerTransform;
 

 void Start () 
 {
     PlayerTransform = transform;
 }
 
 void Update () 
 {                
     //Amount to move, move speed
     float amtToMove = PlayerMoveSpeed * Time.deltaTime;
     
     //Move the Player after they have clicked the Left Mouse Button on a location
     if (Input.GetMouseButtonDown(0))
     {
         Plane playerPlane = new Plane(Vector3.up, PlayerTransform.position);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         float hitdist = 0.0f;
         
         if (playerPlane.Raycast(ray, out hitdist)) 
         {
             Vector3 targetPoint = ray.GetPoint(hitdist);
             TargetPosition = ray.GetPoint(hitdist);
             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
             PlayerTransform.rotation = targetRotation;
         }                        
     }
     
     PlayerTransform.position = Vector3.Lerp(PlayerTransform.position, TargetPosition, amtToMove);
 }

}

This script is attached to my capsule game object. When I run the program, the capsule slides up on the Y-axis to about the center of the screen, then stops. I don't know why the hitdist is set to 0.0f but I suspect that is the problem. But I just don't know what to do with this value as it's passed into the ray.GetPoint function.

Well, most of the script is rather advanced for me. My intention is to have the capsule smoothly move to where I click with the mouse. I do not care about things like collision or terrain at the moment (it's a space game). It's really important I get this basic mechanic working 100% before I continue.

Any help would be much appreciated!

Mac

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by syclamoth · Sep 30, 2011 at 01:06 AM

No, it has nothing to do with hitDist being set to 0. hitDist gets modified in the playerPlane.Raycast line, so that you know how far away the hit was!

The reason why your object moves up to the middle of the screen is because if you don't set a value to TargetPosition by default, it gets set to (0, 0, 0). This makes your character move to the origin, if you do not give it any inputs!

To solve this, set TargetPosition in your Start method, so that it doesn't run away.

 void Start()
 {
     PlayerTransform = transform;
     TargetPosition = PlayerTransform.position;
 }
Comment
Add comment · Show 2 · 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 Mac · Oct 01, 2011 at 07:20 PM 0
Share

Thanks syclamoth. Your suggestion fixed the issue of my object moving to the origin point. I forgot to mention that the "Input.Get$$anonymous$$ouseButtonDown(0)" doesn't result in the object moving to where I click with the left-mouse button.

I moved the line: PlayerTransform.position = Vector3.Lerp(PlayerTransform.position, TargetPosition, amtTo$$anonymous$$ove);

into one of the IF statements but that didn't work either.

avatar image gatorade4 · May 22, 2013 at 11:53 PM 0
Share

I also have the same problem. The object rotates in the right direction but doesn't move.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Click-to-move problems, mouse cursor moving 3 Answers

Move to a clicked point? 1 Answer

GameObject automatically moves up when upgrade is purchased. 2 Answers

mouse clicking on the plane and drag to other position 1 Answer

move UI object to click position 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