Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by onion12345 · Mar 09, 2016 at 12:41 PM · scripting problemcollisionmovementgrid based game

[Unity 2D]Grid Based Character Movement Issues with Vector3D.movetowards

So I am new to C# and Unity...I am trying to create a script that allows the object to move in a grid-fashion (fixed unit displacement of the object per direction input). My current code represents a free movement style...

So here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 {
     public LayerMask BlockingLayer;
     private BoxCollider2D Box_Collider;
     private Rigidbody2D RB2D;
     RaycastHit2D hit;
    
     Vector3 pos;                                // For movement
     float speed = 2.0f;                         // Speed of movement
 
     void Start()
     {
         Box_Collider = GetComponent<BoxCollider2D>();
         RB2D = GetComponent<Rigidbody2D>();
         pos = transform.position;          // Take the initial position
         print(pos);
         
     }
 
     void FixedUpdate()
     {
         DirectionCheck(pos);
         pos = transform.position; 
     }
     void DirectionCheck(Vector3 pos)
     {
         if (Input.GetKey(KeyCode.LeftArrow) && transform.position == pos)
         {
             pos += Vector3.left; // Left
             print(pos);
             CanMove(pos,out hit);
             return ;
         }
              if (Input.GetKey(KeyCode.RightArrow) && transform.position == pos)
             {
             pos += Vector3.right; // Right 
             print(pos);
             CanMove(pos, out hit);
             return ;
         }
                  if (Input.GetKey(KeyCode.UpArrow) && transform.position == pos)
                 {
                     pos += Vector3.up; // Up    
             print(pos);
             CanMove(pos, out hit);
             return;
         }
                     if (Input.GetKey(KeyCode.DownArrow) && transform.position == pos)
                     {
                         pos += Vector3.down; // Down 
            print(pos);
             CanMove(pos, out hit);
             return;
         }
 
         return;
     }
     bool CanMove(Vector3 pos,out RaycastHit2D hit) {
         //print(pos);
         Box_Collider.enabled = false;      
         hit = Physics2D.Linecast(transform.position, pos, BlockingLayer);
         Box_Collider.enabled = true;
         MovePlayer(pos,hit);
         return hit;
     }
     private RaycastHit2D  MovePlayer(Vector3 pos,RaycastHit2D hit)
     {
         if (!hit)
         {
             transform.position = Vector3.MoveTowards(RB2D.position, pos, Time.deltaTime * speed);// Move there
             RB2D.MovePosition(transform.position);
            // print(pos);
             return hit;
        
         }
         else{
             //print(pos);
            
            return hit;
         }
     }
    
 }

I use getkey() to get the input directions to move which is done by : "vector3.right/down/left/up"

I have used Physics2D.Linecast to detect collision at the destination and return true/false with "hit"... then Vector3.movetowards does the moving...but the code ended up not moving in grids....

any directions to how to implement a grid-based movement?

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

0 Replies

· Add your reply
  • Sort: 

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

Movement Colliding Improperly In Two Directions 0 Answers

Cursor collision 0 Answers

No Gravity Jump 0 Answers

how to move a gameobject based on a dice roll 1 Answer

How do I count Hits ?? 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