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 /
avatar image
0
Question by Stregone · Jun 04, 2018 at 11:01 AM · movement scriptclick to move

Getting nowhere with my click to move script

I'm trying to make a script that handles the movement of a unit. I want to be able to select the unit, and then left click again to tell it where to move (I also want to restrict how far the unit can move based on movement points, but that will be a problem for another time.). I also have a UI element telling the unit when the movement phase is active. So far I have the ability to select the unit, let it know what phase the game is in, and register where the mouse clicked. Oh, and I don't want the script to be attached to the unit, as the plan is for it to handle the movement of all the players units.

The problem here is that the unit moves when it is selected AND the game is in the movement phase. Also when clicking in world space the unit only moves a bit and then resets and moves to the same coordinates on the next click.

public class Movement : MonoBehaviour { Mousemanager mousemanager; GameManager gm;

 RaycastHit hit;

 public float speed; //This should maybe be a variable in unit, so that different units have different speeds (should also be different in case of a charge)

 Vector3 difference;
 Vector3 oldDifference;
 Vector3 currentPos;

 // Use this for initialization
 void Start()
 {
     speed = 5f;
     mousemanager = GameObject.FindObjectOfType<Mousemanager>();
     gm = GameObject.FindObjectOfType<GameManager>();
 }

 // Update is called once per frame
 void Update()
 {
     //Click to set target for unit to move to
     if (mousemanager.somethingSelected == true && gm.currentPhase == 1)
     {
         //Calculate how far the mousepointer is from the selected unit (based on local y and z)
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         if (Physics.Raycast(ray, out hit))
         {
             //if (Input.GetMouseButtonUp(0))
             //{
             //    mousemanager.selectedObject.transform.localPosition = Vector3.Lerp(mousemanager.selectedObject.transform.position, hit.point, speed * Time.deltaTime).normalized;
             //}
             
             Moving();

             //store difference in a variable and combare it to new difference. If they are the same, don't print it again

             //make a UI element that follows the mouse pointer here, and tell the player the difference

         }
         //Show this compared to maxMove (maxMove - distance from unit to mousepointer)
     }
 }

 //Unit moves to target
 public void Moving()
 {
     if (Input.GetMouseButtonUp(0))
     {
         mousemanager.selectedObject.transform.localPosition = Vector3.Lerp(mousemanager.selectedObject.transform.position, hit.point, speed * Time.deltaTime);
         //mousemanager.selectedObject.transform.position = Vector3.MoveTowards(currentPos, hit.point, speed * Time.deltaTime);
         if (mousemanager.selectedObject.transform.position.y < 0.5f)
         {
             currentPos.y++;
         }
         Debug.Log("Ready to move");
     }
 }

}

Please help as I'm getting nowhere here.

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

0 Replies

· Add your reply
  • Sort: 

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

83 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RTS Minimap Movement 0 Answers

Space Game - Click To Move and Mouse Look 1 Answer

Idle Animation and Hold to Move 0 Answers

Struggling to smoothly disable diagonal movement 1 Answer

Rotation relativ animation paramter 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