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 atallidiot · Jun 22, 2015 at 03:50 AM · c#movementtransformterrain

How do I check where my character is moving before he moves there?

Hello, and thanks for reading.

I am starting an isometric game that works kinda like pokemon, moving through a space of blocks, but in 3D. I already programed the basic movement, so that the player is able to move the character in the x and z-axis.

In simple words, the problem is that when the player presses a key to move, I need to check the block where the character is moving before he moves, so that I can check if it has another object over it (through a tag in the block called Collision) and that the altitude difference between this block and the current character position isn't too big so that he can walk onto it. Also, if this conditions are met I need to see how much in the y-axis the character needs to move so that he doesn't go through the floor or flies.

here's the function (C#) of the movement:

     void MovementManagement ()
     {
         string dir = "n"; /*direction in which the character is moving: u: up
                                                                         d: down
                                                                         r: right
                                                                         l: left
                                                                         and combinations.
         */
 
         if (canMove && !isMoving) //Checks if the character isn't moving
         {
             //When the player presses a key, we will add a direction to the position
             //using Vector3."anydirection" will add 1 to that direction.
             //also we'll check if we are at the new position, so that the player can
             //only move if he is already in a tile.
 
             if         (Input.GetKey (KeyCode.W) && tr.position == pos)
             {
                 dir = "u";
                 pos += 2 * Vector3.right;
             }
             else if (Input.GetKey (KeyCode.S) && tr.position == pos)
             {
                 dir = "d";
                 pos += 2 * Vector3.left;
             }
             else if (Input.GetKey (KeyCode.D) && tr.position == pos)
             {
                 dir = "r";
                 pos += 2 * Vector3.back;
             }
             else if (Input.GetKey (KeyCode.A) && tr.position == pos)
             {
                 dir = "l";
                 pos += 2 * Vector3.forward;
             }
 
             //if (!the block where the character is moving has the tag 'Collision')
             //{
                 //if (dy < 0.5f) walk0
                 //else if (dy < 1f) walk1
                 //else if (dy < 1.5f) climb
                 //else don't allow movement
                 isMoving = true; //tells the other scripts that the character is moving.
                 
                 transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
                 
                 isMoving = false; //tells the other scripts that the character stopped moving.
             //}
         }
     }

And here's an image of how the demo map works, to illustrate the discrete "movement by blocks", in case I wasn't clear enough.

alt text

Thanks in advance :)

example.jpg (127.7 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
1

Answer by barbe63 · Jun 22, 2015 at 04:53 AM

How I would deal with your project is making an array of array of floats to represent your map. Floats representing the altitude of your walkable tiles or for example -1 if it's unwalkable. Then I would make a level editor to set both of your level and the array by using a text file. You will then just have to make the difference between the tile of your position and the next to allow or offset the animation. Lots of job but as a level editor is a must have in a tile based game I really recommend this. You would most likely gain time in the end.

As another approach you can also use raycast to see if there is an obstacle in the direction you are going to.If nothing is touched, another one directed to the ground with an offset based on the direction you want to catch the tile you want to move on(and therefore its transform.position.y) and do the difference with the character transform.positon.y.

I don't have enough data to decide which is the best. The second is simpler but I'd probably prefer the first for the gain on the long time(and no need of raycasting).

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 atallidiot · Jun 22, 2015 at 05:44 AM 0
Share

I was thinking about doing something like the first option you proposed, though I'm just starting to work on Unity and I'm not an expert on the subject... but I guess nothing bad would come from trying, thanks for the idea :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

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

Moving Terrains causes severe lag, why? 1 Answer

How to allow movement of game object to certain points only 1 Answer

How can I move an object a certain distance, once? 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