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
2
Question by pixelburst · Nov 19, 2011 at 06:59 AM · movementcharactergrid

Character Control - Grid like movement

Hello, I am having a problem getting my character to move the way I would like. I recently purchased the book (Game Development with Unity) and have been following along with an example they have on how they set the character up. This was a great start... however, I am wanting my character to move more like a grid system setup as typically seen in top-down old-school RPG or strategy games. I am new to Java script and any help would be greatly appreciated.

What I have so far...

     //Widget_Controller: Handles Widget's movement and player input
 
 //Widget's Movement Variables------------------------------------------------------------------
 //These can be changed in the Inspector
 
 var rollSpeed = 6.0;
 var gravity = 20.0;
 var rotateSpeed = 4.0;
 
 //private, helper variables-----------------------------------------------------------------------
 
 private var moveDirection = Vector3.zero;
 private var grounded : boolean = false;
 private var moveHorz = 0.0;
 private var normalHeight = 2.0;
 
 private var rotateDirection = Vector3.zero;
 
 var isControllable : boolean = true;
 
 //cache controller so we only have to find it once----------------------------------------------
 
 var controller : CharacterController ;
 controller = GetComponent(CharacterController);
 //var widgetStatus : Widget_Status;
 //widgetStatus = GetComponent(Widget_Status);
 
 //Move the controller during the fixed frame updates------------
 
 function FixedUpdate() {
 
     //check to make sure the character is controllable and not dead
     
     if(!isControllable)
         Input.ResetInputAxes();
     
     else{
         if (grounded) {
         
             // Since we're touching something solid, like the ground, allow movement
             //Calculate movement directly from Input Axes
             
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             if ( moveDirection != Vector3.zero ) 
             transform.rotation = Quaternion.LookRotation( moveDirection ); // This line will allow him to rotate the direction he is moving
             moveDirection *= rollSpeed;
         
             }
         }
         
         // Apply gravity to end Jump, enable falling, and make sure he's touching the ground
         moveDirection.y -= gravity * Time.deltaTime; 
         
         // Move and rotate the controller
         var flags = controller.Move(moveDirection * Time.deltaTime);  
         controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
         grounded = ((flags & CollisionFlags.CollidedBelow) != 0 );
 
        } 
        
        
 //---------------------------------------------------------------------------------------------------------------------------
 
 
 function IsMoving(){
 
     return moveDirection.magnitude > 0.5;
 }
 
 function IsGrounded(){
 
     return grounded;
 }
 
 
 ////Make the script easy to find 
 @script AddComponentMenu("Player/Widget'sController")
Comment
Add comment · Show 2
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 Neten · Nov 19, 2011 at 10:06 AM 0
Share

I'm currently working on a similar game but I've yet to tackle this issue. However, if you are going to use a grid based system like in Final Fantasy I-VI/Pokemon, bear in $$anonymous$$d that those games have your character "snap" to the grid, meaning that there will be no jumping (making IsGrounded obsolete), no rotation other than the 4 main directions.. long story short, what I mean is that using a character controller may not be the most suitable thing to do for such a project.

avatar image Eric5h5 · Nov 19, 2011 at 12:23 PM 0
Share

Please format your code when posting.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Nov 19, 2011 at 12:23 PM

http://www.unifycommunity.com/wiki/index.php?title=GridMove

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 pixelburst · Nov 19, 2011 at 03:01 PM 0
Share

Hi Eric5h5, Thank you for your response. I have looked into the Grid$$anonymous$$ove script and it works great. However, I need it to do one additional thing... I am not sure how I can get it to rotate my character so that it is facing the direction it is moving. I have tried adding something like this... transform.rotation = Quaternion.LookRotation( moveDirection ); but have add zero luck in getting it to rotate my character. Do you know I can get this to work?

avatar image Eric5h5 · Nov 19, 2011 at 08:11 PM 0
Share

There have been lots of questions (and answers) about making rotation match direction of movement; you should be able to find one with a search.

avatar image
0

Answer by pixelburst · Nov 19, 2011 at 09:28 PM

Wanted to post a quick update... I found a solution to my character rotation issue using the GridMove script. I needed to add the following line of code...

 transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
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 Xerosigma · Mar 02, 2012 at 05:19 PM 0
Share

Worked like a charm. Added it to the wiki.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move a character from the middle of one box to the other? 2 Answers

I Watched The Unity New Ball Rolling Game Tut, How I Can Make Jump,I Watched The New Unity Ball Rolling Tutorial, How I Can Make Jump? 0 Answers

Extremely simple movement. 1 Answer

Character Animation Question (Bear Force One) 1 Answer

Character controller + animation of different meshes making one character 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