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 Sir_Waffington · Jul 26, 2014 at 08:52 PM · moveclickwaypointon

2D: How to Move On Click with Creatable (On Click) Waypoints

Hi, I'm trying to figure out how to script a waypoint system (i.e., when you want a unit to flank rather than head straight for the last click) for my 2D characters.

Right now, I have a simple script that causes my character to move toward the last click. What I need is the ability to hold down shift and click to create waypoints. With this, the character will travel to the first click (while shift was held down) before heading to the second.

I've been trying to figure out how to code this, but I'm having some trouble as I am a beginner. If someone could help me by explaining how to create this system for waypoints-on-click (while holding shift), that'd be extremely helpful. Right now I've been making flow charts and brainstorming to see if I could use arrays, etc, but I'm pretty stumped lol.

Thank you for any help.

---------------------------- This is the code for the current movement system.

 public class moveOnMouseClick : MonoBehaviour 
 
 {
     public float moveSpeed;                              // adjustable movement speed of character
     
     private Vector2 destinationPosition;        // The destination Point (Click)
     private Vector2 myPosition;                   // current position of my character
     
     void Start () 
     {
         myPosition = transform.position;       // sets position to the transform.position
     }
     
     void Update () {
         if (Input.GetMouseButtonDown(0)){                                                          // checks for mouse click
             destinationPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);           //  sets mouse position as target destination
         }
         transform.position = Vector2.MoveTowards(transform.position, destinationPosition, moveSpeed * Time.deltaTime);   // moves unit toward destination
     }
 }

I'd also like to mention that the current movement code is also up for critiquing. I'm not sure if that's the best/easiest way to get move-on-click movement, etc. Also, right now the character always moves to the center of the screen at the beginning of the game. I'm not sure lmao. It's only at the beginning; he moves to the center once. After that it works like normal. I can also click while he's moving to the center (the origin, I mean) and he'll move fine and not try going to the center.

I am such a beginner. =P

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 DesmoZ · Jul 31, 2014 at 03:11 AM

Hi, i can specify the way to do it. I don't speak english well, sorry. Declare waypoint array or list of Vector2 with size = 0 (list is better). Then, when you hold shift and click, add new element in array (list) on every click. In void Update() verify, that your transform.position like first element in your waypoint array. Then move your transform to the next element. Good luck

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

move a button On click - rather than on game start 1 Answer

Click to move on a sphere (planetary gravity) 0 Answers

How Can I write a Click To Move Script? 2 Answers

How do I move objects in one executed turn? 1 Answer

Move waypoint mouse click 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