Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 tamas3000 · Dec 06, 2021 at 02:13 PM · scripting beginnerboardgame

working on a board game

Hi everybody,

I am working on a board game and trying to move the player on the fields. I have this code and want to update it to achieve further goals:

 public class playerControl : MonoBehaviour
 
 {
 
 public Transform [] fieldsArray;
 
 
 //public Vector3 offset = new Vector3(-5, 2, -7);
 
 
 int arrayIndex = 0;
 
 
 public float speed = 25;
 
 
 int numberOfMoves = 6;
 
 
 
 
 private float yPosAtStart;
 
 
 private void Start()
 
 {
 
 yPosAtStart = transform.position.y;
 
 transform.position = fieldsArray[arrayIndex].transform.position;
 
 }
 
 
 
 void playerMove()
 
 {
 
 
 
 transform.position = Vector3.MoveTowards(transform.position, fieldsArray[numberOfMoves].transform.position, speed * Time.deltaTime);
 
 if(transform.position == fieldsArray[arrayIndex].transform.position)
 
 {
 
 arrayIndex++;
 
 }
 
 if(arrayIndex == fieldsArray.Length)
 
 {
 
 arrayIndex = 0;
 
 }
 
 
 transform.position = new Vector3(transform.position.x, yPosAtStart, transform.position.z);
 
 
 }
 
 void Update()
 
 {
 
 playerMove();
 
 
 }
 
 
 }

I need some help with two things:

  • I would like to achieve an effect for the player to move from A to B by touching every waypoint. For example now if I change the number of moves to more than 7 the player straight away moves to the 8th field and I do not want it like this. If the number of moves is for example 14 I want to achieve an effect of the player touching every waypoint up to field 14 not just going to the 14th field straight away.

  • So I want the player to move on the fields but with a kind of delay, I want to set up a certain speed and I want the player to touch one field wait a little bit and after a certain amount of time move to the next one and so on.

Thank you very much for the help

Tamas

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 metalted · Dec 06, 2021 at 09:13 PM

Are you sure that the following code is what you want?

  transform.position = Vector3.MoveTowards(transform.position, fieldsArray[numberOfMoves].transform.position, speed * Time.deltaTime);

When the code runs, we instantly move to the field defined in the "numberOfMoves" variable. If you want to start at the first field and 'walk' over all the fields to the destination, you might want to have:

  transform.position = Vector3.MoveTowards(transform.position, fieldsArray[arrayIndex].transform.position, speed * Time.deltaTime);

We now start at field 0, when we arrive we increment "arrayIndex" and move to field 1, repeating until we are at the tile we want.

Quick note: You don't have to call fieldsArray[...].transform.position because every element in the array is already a Transform. fieldsArray[...].position will give you the same value. It's like typing transform.transform.position.

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

133 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 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

How to make grid on circle sprites? 1 Answer

I'm trying to allow my character to only jump when grounded 1 Answer

What should i write that when i press "Shift" it will speed up idk how shift is named? Here's the code: 1 Answer

Changing Postprocessing values via C# script 0 Answers

Do I have this right? Getting a variable value from another script using a JS function and Return 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