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 Razputin · Feb 21, 2014 at 11:12 PM · guimovementtransformpositionturn based

How to use transform.position?

I'm trying to create a turn based game, right now i'm just working on the players movement, heres what i've kind of come up with. using System.Collections; public float speed; public int playerCanMove = 1; public int oldPosition = Transform.position; void Start(){ if(Transform.position != oldPosition) playerCanMove--; } void Update () { if(Input.GetKeyDown(KeyCode.W)) { transform.Translate(0.0f, 0.0f, speed); Transform.position = oldPosition; } else if(Input.GetKeyDown(KeyCode.A)) { transform.Translate(-speed, 0.0f, 0.0f); Transform.position = oldPosition; } else if(Input.GetKeyDown(KeyCode.D)) { transform.Translate(speed, 0.0f, 0.0f); Transform.position = oldPosition; } else if(Input.GetKeyDown(KeyCode.S)) { transform.Translate(0.0f, 0.0f, -speed); Transform.position = oldPosition; } } void OnGUI(){ GUI.Button(new Rect(1200,600,100,100), "End Turn");{ playerCanMove = 1; } }

My question is, how can I properly use transform.position to implement this system? Should I even be approaching it in this way?

Comment
Add comment · Show 3
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 getyour411 · Feb 22, 2014 at 03:28 AM 1
Share

The multiple capital "T" in Transform looks suspect (as in Transform.position); if that's legal the logic is still weird cuz you are moving something with Translate (where the capital T is valid) then assiging the position back seems like it would stutter between the two

avatar image Razputin · Feb 22, 2014 at 03:31 AM 0
Share

Yes, its very possible that my logic may be wrong.

avatar image Razputin · Feb 22, 2014 at 03:33 AM 0
Share

Essentially what i'm trying to do is subtract the moves the player can make by 1, each time the players position changes.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Regalith · Feb 22, 2014 at 01:03 AM

For starters always remember if you're moving something, always multiply it by Time.deltaTime so it'll remain constant on every end-system you're develop. Also you can use Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") for basic movement with alot less code. (this.transform.input.x += Input.GetAxis("Horizontal") * Time.deltaTime) As for the rest, what kind of turn based game are you making? Right now there's no real indication of what the constraints on movement are. For example, is it tile based movement? Whats the maximum a player can move in a turn? You need to consider all this when coming up with how you're going to design your movement system.

Comment
Add comment · Show 5 · 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 Razputin · Feb 22, 2014 at 03:16 AM 0
Share

Its tile based, player can move just once per turn right now, but i'm going to multiply the 1 by some other stats later I think, such as playerCan$$anonymous$$ove =1 * sta$$anonymous$$a; something like that.

avatar image Regalith · Feb 24, 2014 at 01:55 AM 0
Share

Well in that case you could do something like this

 public int gridSize = 1;
 public int sta$$anonymous$$a = 5;
 public float speed = 2;
 bool moving = false;
 bool can$$anonymous$$ove = true;
 
 
 void Update()
 {
    //$$anonymous$$ake Sure the player cant move while already moving
    if(!moving && can$$anonymous$$ove)
    {
       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
       {
          $$anonymous$$ove(1,0);
          sta$$anonymous$$a--;
       }
       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
       {
          $$anonymous$$ove(-1,0);
          sta$$anonymous$$a--;
       }
       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
       {
          $$anonymous$$ove(0,1);
          sta$$anonymous$$a--;
       }
       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
       {
          $$anonymous$$ove(0,-1);
          sta$$anonymous$$a--;
       }
       if(sta$$anonymous$$a <= 0)
          can$$anonymous$$ove = false;
    }
 }
 
 void $$anonymous$$ove(int xDirection, int zDirection)
 {
     moving = true;
 
     while(this.transform.position.x != xDirection && this.transform.position.z != zPosition)
     {
        this.transform.position.x += $$anonymous$$athf.Lerp(this.transform.position.x, this.transform.position.x + xDirection, speed);
        this.transform.position.z += $$anonymous$$athf.Lerp(this.transform.position.z, this.transform.position.z + zDirection, speed);
 
     }
 
     moving = false;
 
 
 }
avatar image Razputin · Feb 24, 2014 at 06:48 PM 0
Share

Hi, Thank you very much for this script i'm going to try to implement it later, currently it's giving me the error zPosition does not exist in the current context and I have to head to work.

avatar image Regalith · Feb 25, 2014 at 04:55 PM 0
Share

it's just giving the idea of how you put together your code. I didn't debug it or spell check it, so just use it as a basis for building your own

avatar image Razputin · Feb 25, 2014 at 07:50 PM 0
Share

Thanks man i figured that out and got it working. ill give you best answer since youve given me the most help.

avatar image
1

Answer by ThomasJWolfe · Feb 22, 2014 at 12:29 AM

This will probably help. http://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate

These videos are a pretty good place to start scripting in general. They are short, but really quality info.

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 Razputin · Feb 22, 2014 at 03:16 AM 0
Share

Thank you I will check it out.

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

21 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

Related Questions

GUI item with object position+dimensions 0 Answers

positioning gui! 2 Answers

How can I call this player transform.position code once, then stop? 2 Answers

Making something move relative to something else, but slower/shorter ? 2 Answers

Intense Lag/Stutter when 3d cube is moving. 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