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 BrandonB · Jun 29, 2013 at 10:52 PM · movementgriditweentransform.translatebased

Grid based movement - using transform.translate

Im trying to get the player to move from tile to tile and i got that to work already with this code. The problem is that is jumps right to the spot and i want to go there at my speed. How its set up now it goes off the x first and then goes off z, so it follows the grid of the game and doesn't just go straight through the grid.


public class Controller : MonoBehaviour { public GameObject selectedPawn;

     public Color inRange;
     public Color outRange;
 
     public float range;
     public float speed;
 
     public bool selected;
 
     private bool standing;
     private bool attacking;
     public bool moving;
 
     private bool showAttackMenu;
     public bool showFacing;
 
     private GameObject[] tiles;
 
     void Start()
     {
         moving = false;
         standing = true;
         attacking = false;
         tiles = GameObject.FindGameObjectsWithTag("Tile");
     }
     public void SetSelectedPawn(GameObject pawn)
     {
         selectedPawn = pawn;
     }
     /*public void Move(GameObject targetTile)
     {
         StartCoroutine("MoveX", targetTile);
     }*/
     public void Move(GameObject targetTile)
     {
         if (!moving)
         {
             selectedPawn.GetComponent<SelectPawn>().ChangeAnim(2);
             //print(moving + "2");
             moving = true;
             float travelTimeOne = targetTile.transform.position.x - selectedPawn.transform.transform.position.x;
             float travelTimeTwo = targetTile.transform.position.z - selectedPawn.transform.transform.position.z;
 
             selectedPawn.transform.Translate(travelTimeOne,0,0,Space.World);
             selectedPawn.transform.Translate(0, 0, travelTimeTwo, Space.World);
 
 
            /* iTween.MoveBy(selectedPawn, iTween.Hash("x", targetTile.transform.position.x - selectedPawn.transform.position.x,
                 "easetype", "linear", "time", travelTimeOne));
 
             iTween.MoveBy(selectedPawn, iTween.Hash("z", targetTile.transform.position.z - selectedPawn.transform.position.z,
                 "time", travelTimeTwo, "delay", travelTimeOne, "easetype", "linear"));*/
             
             DoneMoving();
             CheckTiles(2);
             //selectedPawn.SendMessage("DoneMoving");
             moving = false;
             selectedPawn.GetComponent<SelectPawn>().ChangeAnim(1);
 
         }
         
     }
Comment
Add comment · Show 2
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 Em3rgency · Jun 30, 2013 at 08:05 AM 0
Share

This is getting dangerously close to pathfinding. Which is a complicated as hell topic. People have gotten degrees by writing papers on pathfinding algorithms. This is probably why your question lacks answers :)

avatar image Cyber-X-Zone · Aug 17, 2013 at 12:17 PM 0
Share

I noticed that in these lines,

  float travelTimeOne = targetTile.transform.position.x - selectedPawn.transform.transform.position.x;
  float travelTimeTwo = targetTile.transform.position.z - selectedPawn.transform.transform.position.z;

you have written tranform 2 times in selectedPawn.transform.transform.position.x and selectedPawn.transform.transform.position.z.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cyber-X-Zone · Aug 17, 2013 at 01:12 PM

While I am not sure about this and could only suggest you, but by looking at your script, I think you can use Vector3.Lerp, which is used for linearly interpolating between two vectors. Here's its format:

 static function Lerp(from: Vector3, to: Vector3, t: float): Vector3;

You could set from as the position of the pawn at present and to as the position of the desired tile.

 float journeyLength = Vector3.Distance(targetTile.transform.position, selectedPawn.transform.position);;


And then, just write the code as:

  public void Move(GameObject targetTile)
     {
         if (!moving)
         {
           selectedPawn.GetComponent<SelectPawn>().ChangeAnim(2);
           //print(moving + "2");
           moving = true;
           float fracJourney = distCovered / journeyLength;
           transform.position = Vector3.Lerp(targetTile.transform.position, selectedPawn.transform.position, fracJourney);
         }
     }
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 Glurth · Dec 30, 2014 at 05:29 PM 0
Share

I like this answer, but I would also use Update() and Time.deltaTime to animate distCovered.

avatar image
0

Answer by Sajidfarooq · Aug 17, 2013 at 07:01 PM

Actually, as you seem to know how to use iTween, there is an elegant way to do this.

Your movement is broken down in two dimensions: a) horizontal, then b) vertical (or vice-versa). What you need is to run a), wait for it to finish, then launch b). By default however, both tweens will run at the same time if you run them from the same function.

Luckily, iTween provides a callback "oncomplete" that allows you to call-back a function when a tween has finished. So:

 public void Move()
 {
 iTween.MoveBy(selectedPawn, iTween.Hash("x", newXPos,"time", travelTimeOne, "oncomplete", "EndHorizontalMove"));
 }
 
 public void EndHorizontalMove()
 {
 iTween.MoveBy(selectedPawn, iTween.Hash("z", newZPos,"time", travelTimeOne)

  DoneMoving();
 }
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
avatar image
0

Answer by AlexKostas · Oct 02, 2017 at 04:12 PM

You can use Vector3.MoveTowards

For example:

transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);

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

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

Faults with Grid-Based movement... 2 Answers

Smooth Grid Movement 0 Answers

Adding collision detection to movement script... 1 Answer

Manual Collision Detection through triggers... 0 Answers

Move along grid where facing 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