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 alpayoguz97 · Jun 22, 2021 at 10:33 AM · movementgridmove

How to move object grid base continuously?

I want to move object continously when I press button just once grid to grid. When I press button to make object move to another direction, it should wait until object is right on the grid and starts to move another direction. Those are my codes. It moves continously when I press once but doesn't move grid to grid

 void GridMove()
     {
         Debug.Log("ye");
 
         if(Input.GetKeyDown(KeyCode.W))
         {
             forward = true;
             back = false;
             left = false;
             right = false;
 
         }
 
 
         
         if (forward)
         {
             StartCoroutine(MoveCharacter(Vector3.forward * Time.deltaTime));
         }
 
         if (Input.GetKeyDown(KeyCode.S))
         {
             back = true;
             forward = false;
             left = false;
             right = false;
 
 
         }
 
 
         if (back)
         {
             StartCoroutine(MoveCharacter(Vector3.back * Time.deltaTime));
         }
 
         if (Input.GetKeyDown(KeyCode.A))
         {
             left = true;
             back = false;
             forward = false;
             right = false;
             
 
         }
 
 
         if (left)
         {
             StartCoroutine(MoveCharacter(Vector3.left * Time.deltaTime));
         }
 
 
         if (Input.GetKeyDown(KeyCode.D))
         {
             right = true;
             back = false;
             forward = false;
             left = false;
 
 
         }
 
 
         if (right)
         {
             StartCoroutine(MoveCharacter(Vector3.right * Time.deltaTime));
         }
 
 
 
 
 
 
 
 
 
 
 
 
     }
 
 
     private IEnumerator MoveCharacter(Vector3 direction)
     {
         
 
         float elapsedTime = 0f;
 
         originPos = transform.position;
         targetPos = originPos + direction;
 
 
         while (elapsedTime < timeToMove)
         {
             transform.position = Vector3.Lerp(originPos, targetPos, (elapsedTime / timeToMove));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
 
 
         transform.position = targetPos;
 
 
 
         
     }


alt text

resim-2021-06-22-133258.png (242.5 kB)
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 DenisIsDenis · Jun 22, 2021 at 11:50 AM

In general, after looking at your code, I thought that a different approach is needed here. In the following script, I did something like what you need. Comments in the script can help to understand.

 using UnityEngine;
 
 public class CubeMovingOnGrid : MonoBehaviour
 {
     //It is much easier to use a direction vector
     //than four bool variables and four conditions.
     Vector3 movingDirection = Vector3.forward;
 
     //We use end position to snap to grid.
     Vector3 targetPosition;
 
     float timeToMove = 5f;
     float elapsedTime;
 
     //Grid cell size
     public float lengthCellGrid = 1;
     public float speed = 1;
 
     void Start()
     {
         targetPosition = transform.position;
     }
 
     void Update()
     {
         GridMove();
     }
 
     void GridMove()
     {
         elapsedTime += Time.deltaTime;
 
         if (Input.GetKeyDown(KeyCode.W))
         {
             movingDirection = Vector3.forward * lengthCellGrid;
         }
         if (Input.GetKeyDown(KeyCode.S))
         {
             movingDirection = Vector3.back * lengthCellGrid;
         }
         if (Input.GetKeyDown(KeyCode.A))
         {
             movingDirection = Vector3.left * lengthCellGrid;
         }
         if (Input.GetKeyDown(KeyCode.D))
         {
             movingDirection = Vector3.right * lengthCellGrid;
         }
 
         // Shift targetPosition if we got to it
         if (targetPosition == transform.position && elapsedTime < timeToMove)
         {
             targetPosition += movingDirection;
         }
 
         // Moving to targetPosition
         transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
     }
 }
Comment
Add comment · Show 6 · 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 alpayoguz97 · Jun 22, 2021 at 01:41 PM 0
Share

Thanks for answer. But this is not actually what I want. But ıt's not working as I want

avatar image DenisIsDenis alpayoguz97 · Jun 22, 2021 at 01:42 PM 0
Share

Then clarify what do you need?

avatar image alpayoguz97 DenisIsDenis · Jun 22, 2021 at 02:02 PM 0
Share

For example when object tries to turn right when its going forward, ıt should wait until grid ends. But not according to time. I used time for it but its not logical. I want object to know wheter its in grid or not. Should I use layer mask or something like that ?

Show more comments
avatar image DenisIsDenis alpayoguz97 · Jun 24, 2021 at 05:14 AM 0
Share

I probably figured out what you need. If I'm not mistaken, you need the following:

1) The player controls the character, and when he stops, the cube stops at a certain cell exactly in the middle.

2) You can turn the cube only when it is exactly in the middle of the cell, in order to avoid walking between cells.

If so, then by modifying my first script a little, you can get the desired result:

 using UnityEngine;
 
 public class CubeMovingOnGrid : MonoBehaviour
 {
     //It is much easier to use a direction vector
     //than four bool variables and four conditions.
     Vector3 movingDirection = Vector3.forward;
 
     //We use end position to snap to grid.
     Vector3 targetPosition;
 
     //Grid cell size
     public float lengthCellGrid = 1;
     public float speed = 1;
 
     void Start()
     {
         targetPosition = transform.position;
     }
 
     void Update()
     {
         GridMove();
     }
 
     void GridMove()
     {
         if (Input.GetKeyDown(KeyCode.W))
         {
             movingDirection = Vector3.forward * lengthCellGrid;
         }
         if (Input.GetKeyDown(KeyCode.S))
         {
             movingDirection = Vector3.back * lengthCellGrid;
         }
         if (Input.GetKeyDown(KeyCode.A))
         {
             movingDirection = Vector3.left * lengthCellGrid;
         }
         if (Input.GetKeyDown(KeyCode.D))
         {
             movingDirection = Vector3.right * lengthCellGrid;
         }
 
         // Shift targetPosition if we got to it
         bool isGoing = Input.GetButton("Horizontal") || Input.GetButton("Vertical");
         if (targetPosition == transform.position && isGoing)
         {
             Ray ray = new Ray(transform.position + Vector3.up + movingDirection, Vector3.down);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 2))
             {
                 if (hit.collider.CompareTag("Floor"))
                 {
                     targetPosition += movingDirection;
                 }
             }
         }
 
         // Moving to targetPosition
         transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
     }
 }

(Note: cells of the floor must be marked with the tag "Floor" and added to them some kind of collider (you can use one collider for all cells at once))

avatar image alpayoguz97 DenisIsDenis · Jun 24, 2021 at 05:36 AM 0
Share

Thanks really. This is exactly what I want I think. I'm gonna try implement this

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

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

Having trouble with grid movement 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

3D Grid Movement 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Grid-based movement with auto-move 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