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 Yharooer · Jul 11, 2013 at 07:43 AM · javascriptmovementgridlocomotion

Grid Based Movement (Pokemon-like)

Hi,

I'm trying to write a script which will move the player depending on the value of a static variable, which is controlled by other scripts.

I have tried to get it to work, but am failing so badly, so I decided to put it here and see if anyone knows where I am going wrong.

So far the character only travels in one direction, which is ok, but I can only move him forward one time and then he won't move.

I would like this movement to be like the character moves in Pokemon games.

This is starting to drive me crazy so any help would be much appreciated.

Here is my code so far:

KeyboardInput.js

 #pragma strict
 
 function Update () {
     if (Input.GetKey(KeyCode.UpArrow)){
         PlayerMove.directionPress = 1;
     }
     else if (Input.GetKey(KeyCode.RightArrow)){
         PlayerMove.directionPress = 2;
     }
     else if (Input.GetKey(KeyCode.DownArrow)) {
         PlayerMove.directionPress = 3;
     }
     else if (Input.GetKey(KeyCode.LeftArrow)){
         PlayerMove.directionPress = 4;
     }
     else if (Input.GetKey(KeyCode.W)){
         PlayerMove.directionPress = 1;
     }
     else if (Input.GetKey(KeyCode.D)){
         PlayerMove.directionPress = 2;
     }
     else if (Input.GetKey(KeyCode.S)) {
         PlayerMove.directionPress = 3;
     }
     else if (Input.GetKey(KeyCode.A)){
         PlayerMove.directionPress = 4;
     }
     //All other keys here.
     else {
         PlayerMove.directionPress = 0;
     }
 }



PlayerMove.js

 #pragma strict
 
 static var directionPress : int = 0; //0= none 1 = up, 2= right, 3= down, 4= left
 private var movingDirection : int = 0;
 var moveSpeed : float = 5; //units per second
 var isMoving : boolean = false;
 
 
 class TileTrigger{ //class for 
     var isOccupied : boolean;
     var hasFloor : boolean;
 }
 
 var tileNorth = TileTrigger();
 var tileEast = TileTrigger();
 var tileSouth = TileTrigger();
 var tileWest = TileTrigger();
 
 function Update() {
     Debug.Log("directionPress = " + directionPress + ". movingDirection = " + directionPress + ".");
     Debug.Log("transform.position.z = " + transform.position.z);
     Debug.Log("isMoving = " + isMoving);
     if (!(isMoving) && movingDirection == 0 && directionPress != 0) {
         movingDirection = directionPress;
         isMoving = true;
     }
 }
 
 function LateUpdate () {
     if (isMoving) {
         if (movingDirection == 1) {
             if (parseInt(transform.position.z + (Vector3.forward*Time.deltaTime * moveSpeed).z) >= parseInt(transform.position.z)) {
                 // It has gone over onto the next square.
                 if (directionPress == movingDirection) {
                     MoveForward();
                     // if its moving in the same direction, then move normally.
                 }
                 if (directionPress != movingDirection) {
                     if (directionPress == 0) {
                         isMoving = false;
                         transform.position.z = parseInt(transform.position.z + 1);
                         // if no button press, then move to the next square.
                         Debug.Log("Moving onto the next square.");
                     }
                     else {
                         transform.position.z = parseInt(transform.position.z + 1);
                         ChangeRot();
                         // if another key pressed, then move to next square and rotate.
                         // then next frame the norm happens.
                         Debug.Log("Stopping at this square.");
                     }
                 }
             
             } //the end of if it has gone over to the next square.
             else {
                 MoveForward();
             }
         } //the end of if movingDirection == 1
         
         if (movingDirection == 0) {
             isMoving = false;
         }// the end of if movingDirection == 0
     } //end of isMoving
     Debug.Log(" ");
 }
 
 function MoveForward () {
     transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
 }
 
 function ChangeRot() {
     Debug.Log("ChangeRot() just happened."); //but this never happens. :(
     movingDirection = directionPress;
     if (movingDirection != 0) {
         transform.rotation.z = (movingDirection - 1) * 90;
     }
 }
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
1

Answer by DavidDebnar · Jul 11, 2013 at 09:14 AM

Grid based movement

--David--

Comment
Add comment · Show 2 · 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 Yharooer · Jul 11, 2013 at 09:51 AM 0
Share

None of the results are actually useful though....

avatar image DavidDebnar · Jul 11, 2013 at 09:56 AM 0
Share

And what about Grid$$anonymous$$ove?

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

16 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

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Make Lerp or other more fluid or continuous 3 Answers

I am trying to make a movement script for my main camera but it isnt working! 2 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