Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by eevachan · Feb 18, 2016 at 09:20 AM · beginner

Character movement with click

I'm a completely beginner at game developing.
I've browse through tutorials on internet about 2d game with unity, but mostly its about platformer or something similar (movements are right-left-jump, means not the one i'm looking for).
What i wanna make is 'diner dash' like game, when player click on a table(or anything) the player is move toward the object, and do something (in diner dash case, she take customer's order). and most of the tutorial that i've watched the controller is using keyboard. So, any suggestion where i have to start? Any keyword for google search?
sorry for my engrish :(

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
0

Answer by Salmjak · Feb 18, 2016 at 12:06 PM

To translate mouse position to world-coordinates you would want to use a Raycast. So that's how you will know where the player clicks. The code would look like this (you can also check out this video):

 float speed = 10f;
 float rotationSpeed = 20f;
 
 void Update () {
          if (Input.GetMouseButton(0))
                 {
                     RaycastHit hit;
                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                     if (Physics.Raycast(ray, out hit)
                     {
                         Vector3 newPos = Vector3.MoveTowards(transform.position, hit.point, Time.deltaTime * speed);
                             Vector3 _direction = (hit.point - transform.position).normalized;
                             Quaternion lookDirection = Quaternion.LookRotation(_direction);
                             lookDirection.x = 0;
                             lookDirection.z = 0;
                             gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, lookDirection, Time.deltaTime * rotationSpeed);
                             transform.position = newPos;
                            }
                  }
     }
 

In the above code we first check if the player is pressing the left mouse button. Then we create a ray using the mouse position (ScreenPointToRay translated the screen point to a world point and direction). If use the ray in a Raycast, which will return a RaycastHit (hit) if it hits something. We then create a new position, that will be somewhere between transform.position and the place we hit with a max distance of Time.deltaTime * speed (you will have to set the speed as a float yourself). Then we use some quaternions and normalized vectors to get a direction to look at. This is not needed for the movement, but it looks nice when you character turns.

And finally we set our transform.position to the new position, and since this is in the Update()-function it will run every frame and you will move closer and closer to the goal.

And for a move on click function you would loop this code after the player releases the button. It would look like this:

 void Update(){
  if (Input.GetMouseButtonUp(0))
             {
                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                     RaycastHit hit;
                     if (Physics.Raycast(ray, out hit))
                     {
                             StartCoroutine(MoveToPoint(hit.point));
                       }
               }
 }
 
 float speed = 10f;
 float rotationSpeed = 20f;
 IEnumerator MoveToPoint(Vector3 newPos)
     {
         Vector3 _direction = (newPos - transform.position).normalized;
         Quaternion lookDirection = Quaternion.LookRotation(_direction);
         lookDirection.x = 0;
         lookDirection.z = 0;
         while (Vector3.SqrMagnitude(transform.position - newPos) > 0.5f)
         {
             Vector3 nextPos = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * speed);
             gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, lookDirection, Time.deltaTime * rotationSpeed);
             transform.position = nextPos;
             yield return new WaitFixedUpdate();
         }
     }

Where you again will have to set the float speed and float rotationSpeed youself.

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 eevachan · Feb 22, 2016 at 03:35 PM 0
Share

is it works in 2D also?

avatar image Salmjak eevachan · Feb 23, 2016 at 01:34 PM 0
Share

Define "2D", your description sound like a top-down game, so yes. It will not work on a platformer (which you said you already have tutorials for).

It CAN work in 2D-platformer style if you rework it somewhat, replace Vector3 with Vector2 and find a good way to the find the correct position to move to (so you don't magically move up in the air), you can still use Raycast as I did but make sure to add the suffix "2D" to all physic-function (ex. Rigidbody2D, Raycast2D, RaycastHit2D, etc). But the basis of looping the movement is still the same :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

standard Assest 1 Answer

How to substitute one object with another in animation clip 0 Answers

Why is my Unity different than the one in the Beginner Tutorial? 1 Answer

Accessing Channel Values in Color Correction Curves 0 Answers

I have starded the basic tutorial and got this bug. Cannot move further because of this. 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