• 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 ThatGuyMiniB · Oct 08, 2015 at 10:19 AM · c#movement3dgridgridmove

Having trouble with grid movement

So I am trying to get grid movement to work properly I am aiming for movement kind of like crossy roads. I have this code so far but if the player presses their movement to fast it goes off of the grid so instead of moving by 1 in x direction it will move by some decimal number. Any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class NewPlayer : MonoBehaviour {
     float lerpTime;
     float currentLerpTime;
     float perc = 1;
     public static Transform playerTransform;
 
     Vector3 startPos;
     Vector3 endPos;
 
     bool firstInput;
     public bool justJump;
 
     public bool moveUp = true;
     public bool moveDown = true;
     public bool moveLeft = true;
     public bool moveRight = true;
     public bool didTouch = false;
     public bool canPush = false;
 
     public GameObject raycastObject;
 
 
 
     void Update() {
 
                 }
 
     void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "UpStop"){
             moveUp = false;
             canPush = true;
         }
         if (other.gameObject.tag == "WallUpStop"){
             moveUp = false;
             print ("BOOYA");
             //canPush = true;
         }
         if (other.gameObject.tag == "RightStop"){
             moveRight = false;
             canPush = true;
         }
         if (other.gameObject.tag == "LeftStop"){
             moveLeft = false;
             canPush = true;
         }
         if (other.gameObject.tag == "DownStop"){
             moveDown = false;
             canPush = true;
         }
 
     }
 
     void OnTriggerExit(Collider other){
         if (other.gameObject.tag == "UpStop"){
             moveUp = true;
             canPush = false;
         }
         if (other.gameObject.tag == "WallUpStop"){
             moveUp = true;
             print ("BOOYA");
             //canPush = true;
         }
         if (other.gameObject.tag == "RightStop"){
             moveRight = true;
             canPush = false;
         }
         if (other.gameObject.tag == "LeftStop"){
             moveLeft = true;
             canPush = false;
         }
         if (other.gameObject.tag == "DownStop"){
             moveDown = true;
             canPush = false;
         }
     }
 
 
 
 
     
     
     // Update is called once per frame
     void FixedUpdate () {
 
 
         if (Input.GetButtonDown ("up") || Input.GetButtonDown ("down") || Input.GetButtonDown ("left") || Input.GetButtonDown ("right")) {
             if (perc == 1) {
                 lerpTime = 1;
                 currentLerpTime = 0;
                 firstInput = true;
                 justJump = true;
             }
         }
         startPos = gameObject.transform.position;
         //&& gameObject.transform.position == endPos
 
         if (Input.GetButtonDown ("right")  && moveRight == true)
         {
             endPos =  new Vector3 (transform.position.x + 1, transform.position.y, transform.position.z );
 
         }
         if (Input.GetButtonDown ("left")  && moveLeft == true)
         {
             endPos = new Vector3 (transform.position.x - 1, transform.position.y, transform.position.z );
 
         }
         if (Input.GetButtonDown ("up")  && moveUp == true)
         {
             endPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z + 1);
 
         }
 
         if (Input.GetButtonDown ("down")  && moveDown == true)
         {
             endPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z - 1);
 
         }
 
         if (Input.GetButtonDown ("right"))
         {
             gameObject.transform.rotation = Quaternion.Euler (0,90,0);
         }
         if (Input.GetButtonDown ("left"))
         {
             gameObject.transform.rotation = Quaternion.Euler (0,-90,0);
 
         }
         if (Input.GetButtonDown ("up"))
         {
             gameObject.transform.rotation = Quaternion.Euler (0,0,0);
 
         }
         if (Input.GetButtonDown ("down"))
         {
             gameObject.transform.rotation = Quaternion.Euler (0,180,0);
 
         }
 
 
         if (firstInput == true) 
         {
             currentLerpTime += Time.deltaTime * 5.5f;
             perc = currentLerpTime / lerpTime;
             gameObject.transform.position = Vector3.Lerp (startPos, endPos, perc);
         if (perc > 0.8)
             {
                 perc = 1;
             }
             if (Mathf.Round(perc) == 1)
             {
                 justJump = false;
             }
         }
 
     }
 }
 
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 Gilles_aerts · Oct 08, 2015 at 02:57 PM

This "problem" you are experiencing is because in theory a lerped position will never reach his destination. The best solution to this is the move an empty GameObject (tile based as you are doing in your script) and then make your player lerp to that empty GameObject.

You might want to look up what the function lerp exactly does in order to understand this solution. but i'll give you and example :

lets say your player is on the position (0,0,0) .. in your code you ask it to lerp to position (1,1,0) , since you lerp the position it will never reach position (1,1,0) but it will reach something like (0.997,0.997,0) if you ask your player to move again by 1 unit you start from that (0.997,0.997,0) position and will end up something like (1.836,1.836,0) so its no longer tile based.. each time you move your player your position will get more 'messed up'

Let me know if this is not clear, then i'll try to explain it on an other way

Good luck

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Grid based movement in 3D environment 1 Answer

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

How to move character in a grid-based area? 1 Answer

How do i maintain the same speed in the air? 1 Answer

Help with recreating movement from game 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