Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by sullivanshad · Nov 27, 2013 at 04:31 PM · c#dungeon

method for grid movement

I'm currently working on a rouguelike, where I want movement similar to legend of grimrock, or the newer chocobo's dungeon game. I've tried using iTween, lerp, grid move, and moveTowards to move a test block in the dungeon, but i run into at least one of 4 problems.

  1. the block teleports to the area, rather than moving smoothly.

  2. the block doesn't move at all

  3. the block move's smoothly, but when you take your finger off the key the block stops before centering on the next tile, or goes past it slightly.

  4. the code doesn't properly check for collisions

I have some code I worked on recently, using moveTowards, and I get problem 3:

using UnityEngine; using System.Collections;

 public class controllerscript : MonoBehaviour {
     // Use this for initialization
     void Start () {
     
     }
 
 // Update is called once per frame
 void Update () {
     bool moving= false;
     Vector3 endposition= transform.position+ (Vector3.forward);
     if(Input.GetKey(KeyCode.W) && moving==false){
         moving=true;
         transform.position=Vector3.MoveTowards(transform.position, endposition , Time.deltaTime);
         moving=false;
     }
 }

}

I'd like the cube to snap to the tiles like with typical grid movement, but so far I haven't had any luck. Any ideas?

Thanks in advance!

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
0
Best Answer

Answer by robertbu · Nov 27, 2013 at 04:49 PM

Here is a rewrite of your code that solve most of your issues. Depending on your game, this may have issues with collisions. If so, look at Rigidbody.MovePosition() to solve the issue:

 using UnityEngine;
 using System.Collections;
 
 public class ControllerScript : MonoBehaviour {
     public float speed = 1.0f;
 
     private Vector3 endpos;
     private bool moving = false;
 
     void Start () {
         endpos  = transform.position;
     }
     
     void Update () {
         if (moving && (transform.position == endpos))
             moving = false;
 
         if(!moving && Input.GetKey(KeyCode.W)){
             moving = true;
             endpos = transform.position + Vector3.forward;
         }
 
         transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
     }
 }
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 sullivanshad · Nov 27, 2013 at 08:28 PM 0
Share

The code works perfectly, even with collisions. Thanks so much :D

avatar image sycr99 · Nov 05, 2020 at 09:30 AM 0
Share

Hi .. I have an object that moves in the form of a grid, by touching it on the phone. How do I write code to move the unit left, right and forward?

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

17 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Distribute terrain in zones 3 Answers

Unity: C#: Eyes look at mouse: HELP!! 3 Answers

C# how to make Character Motor Jump Infinitely Upward 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