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 Oobi23 · Apr 16, 2013 at 01:17 AM · movementcharactergridcharacter movement

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

So basically we are creating a game which requires the character to move on top of boxes that are kept together like a 3d plane. we got the character moving in fluid motion but that's not what we want. So how to make the character move from middle of one box to another in pixelated sort of movement. Thanks

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 robertbu · Apr 16, 2013 at 01:58 AM 0
Share

How do you want to initiate movement? Do you want instant movement or movement over time? Can you show us your current script for movement?

avatar image Oobi23 · Apr 16, 2013 at 03:06 AM 0
Share

instant movement with the arrow keys, but the movement has to be like a frame by frame effect where we refer to different positions of the character, which are flat .png files as our camera angle is fixed in a orthographic view (so there's no need for a 3D character).

    /// This script moves the character controller forward 
     /// and sideways based on the arrow keys.
     /// It also jumps when pressing space.
     /// $$anonymous$$ake sure to attach a character controller to the same game object.
     /// It is recommended that you make only one call to $$anonymous$$ove or Simple$$anonymous$$ove per frame.    
     var speed : float = 6.0;
     var jumpSpeed : float = 8.0;
     var gravity : float = 20.0;
     private var moveDirection : Vector3 = Vector3.zero;
     function Update() {
         var controller : CharacterController = GetComponent(CharacterController);
         if (controller.isGrounded) {
             // We are grounded, so recalculate
             // move direction directly from axes
             moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                     Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
             }
         }
         // Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
         
         // $$anonymous$$ove the controller
         controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Apr 16, 2013 at 01:49 PM

You are using a character conroller for movement. For instant movement you only have to translate the character. I would also explicitly use the arrow keys if that is what you want rather than GetAxis(). So you will need four 'if' statements (one for each direction.) Here is an example of one direction:

 if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.z < max_z_value) {
     transform.position += Vector3.forward;
 }

This code assumes the blocks are 1 unit on a side.

I don't know what a "frame by frame effect" is. I guessing you want to change the texture of the character based on the direction of the key pressed. You can do that by having an array of textures and setting the mainTexture. See Material.mainTexture.

Comment
Add comment · Show 4 · 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 Oobi23 · Apr 22, 2013 at 12:56 PM 0
Share

hi there, I understand the coding bit you have presented. But I might need some help on how to use it in my movement javascript. where do i include this coding? I am new to coding. Thanking you in advance. and the coding is giving an error with the max_z_value.. so confused..

avatar image robertbu · Apr 22, 2013 at 02:16 PM 0
Share

max_z_value is a variable you need to define and initialize that is the maximum value you want to allow the character to move. It will be at or slightly under the center position of the block most forward. For a 10 x 10 grid centered on the origin, the value would be 4.9. For example at the top of the file:

 private var max_z_value = 4.9;
 private var $$anonymous$$_z_value = -4.9;
 private var max_x_value = 4.9;
 private var $$anonymous$$_x_value = -4.9;

And then the code inline would be:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) && transform.position.z < max_z_value) {
     transform.position += Vector3.forward;
 }
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow) && transform.position.x > $$anonymous$$_z_value) {
     transform.position += Vector3.back;
 }
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow) && transform.position.x < max_x_value) {
     transform.position += Vector3.Right;
 }
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow) && transform.position.x < $$anonymous$$_x_value) {
     transform.position += Vector3.Left;
 }

avatar image Oobi23 · Apr 24, 2013 at 03:40 PM 0
Share

works like a charm with some modifications.. thanks :D

avatar image robertbu · Apr 24, 2013 at 08:37 PM 0
Share

If your question is answered, pick the best answer and click on the check mark to close out the question.

avatar image
0

Answer by anniyan137 · Apr 22, 2013 at 01:41 PM

I am assuming you're very new to Unity. (and programming too?)

That said, my suggestion would be to learn a bit of coding. Go through the tutorials on the Unity website.

Apart from that, what you would need to do is skip using the CharacterController and just do the calculations by hand. It should look something like this.

 var x_offset : float = <some value>; // By how much the character should move in the X direction
 var y_offset : float = <some value>; // By how much the character should move in the Y direction
 var newPos : Vector3;
 
 .
 .
 .
 
 if( Input.GetKeyDown(KeyCode.RightArrow) )
 {
     newPos = new Vector3(transform.position.x + x_offset, transform.position.y, transform.position.z);
     transform.Translate( newPos );
 }
 if( Input.GetKeyDown(KeyCode.LeftArrow) )
 {
     newPos = new Vector3(transform.position.x - x_offset, transform.position.y, transform.position.z);
     transform.Translate( newPos );
 }
 if( Input.GetKeyDown(KeyCode.UpArrow) )
 {
     newPos = new Vector3(transform.position.x, transform.position.y + y_offset, transform.position.z);
     transform.Translate( newPos );
 }
 if( Input.GetKeyDown(KeyCode.DownArrow) )
 {
     newPos = new Vector3(transform.position.x, transform.position.y + y_offset, transform.position.z);
     transform.Translate( newPos );
 }
 
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

12 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

Related Questions

character(ball) rolls with less speed when I build it 2 Answers

Character Control - Grid like movement 2 Answers

Grid-based movement: How to face forward 2 Answers

How does character selection in endless runner works? 0 Answers

simple character movement 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