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 Smazzolus · Sep 21, 2014 at 04:00 PM · movementmousegridclickturn based

Help with turn based Movement in a grid

Hello everyone, I'm new and I need some advice on how to implement the grid system movement using the mouse, a bit like in the series of Heroes of Might and Magic. I created the battlefield using this script that generates tiles: Grid.js

 public var tilePrefab: GameObject;
 public var numberOfTiles: int = 10;
 public var tilesPerRow: int = 4;
 public var distanceBetweenTiles: float = 1.0;
 
  
 function Start()
 {
     CreateTiles();
 }
  
 function CreateTiles()
 {
     var xOffset: float = 0.0;
     var zOffset: float = 0.0;
  
     for(var tilesCreated: int = 0; tilesCreated < numberOfTiles; tilesCreated += 1)
     {
         xOffset += distanceBetweenTiles;
          
         if(tilesCreated % tilesPerRow == 0)
         {
             zOffset += distanceBetweenTiles;
             xOffset = 0;
         }
      
         Instantiate(tilePrefab, Vector3(transform.position.x + xOffset, transform.position.y, transform.position.z + zOffset), transform.rotation);
     }
 }

Then I created a script to give to an object, called Target, which basically allows you to move it into the "grid" using the mouse. Target.cs

 using UnityEngine;
 using System.Collections;
 
 public class Target : MonoBehaviour
 {
 
     public bool selected = false;
     public float floorOffset = 1; 
     public float speed = 5;
     public float stopDistanceOffset = 0.5f;
     
     private Vector3 moveToDest = Vector3.zero;
     private bool selectedByClick = false;
     
     private void Update ()
     {
     
 //        if (renderer.isVisible && Input.GetMouseButton(0))
 //        {
 //            if (!selectedByClick)
 //            {
 //                Vector3 camPos = Camera.mainCamera.WorldToScreenPoint(transform.position);
 //                //camPos.y = Mouse.InvertMouseY(camPos.y);
 //                //selected = Mouse.selection.Contains(camPos);
 //            }
             
 //            if (selected)
                 renderer.material.color = Color.red;
 //            else
                 renderer.material.color = Color.white;
 //        }
         
         if (selected && Input.GetMouseButtonDown(1))
         {
             Vector3 destination = Mouse.GetDestination();
             
             if (destination != Vector3.zero)
             {
                 //gameObject.GetComponent<NavMeshAgent>().SetDestination(destination);
                 
                 moveToDest = destination;
                 moveToDest.y += floorOffset;
             }
             
         }
         if (selected && Input.GetMouseButtonDown(0))
         {
             selected = false;
 
             
         }
 
         UpdateMove();
     }
     
     private void UpdateMove()
     {
         if (moveToDest != Vector3.zero && transform.position != moveToDest)
         {
             Vector3 direction = (moveToDest - transform.position).normalized;
             direction.y = 0;
             transform.rigidbody.velocity = direction * speed;
             
             if (Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset)
                 moveToDest = Vector3.zero;
         }
         else
             transform.rigidbody.velocity = Vector3.zero;
             
     }
 
     private void OnMouseDown()
     {
         selectedByClick = true;
         selected = true;
     }
     
     private void OnMouseUp()
     {
         if (selectedByClick)
             selected = true;
         
         selectedByClick = false;
     }
 
     
 }


The only thing is that the movement appears to be "free", not connected to the grid.

How can I do so that it is connected to the grid? So that the object moves per block ??

Thanks and sorry for my bad English I hope I was clear enough

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SirLaur · Jan 04, 2018 at 09:39 AM

Hello, sorry for asking in such an old thread, but did u manage to make it work ? Thanks

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

Answer by Smazzolus · Jan 04, 2018 at 01:44 PM

Hello! No, I gave up and bought the TBTK asset by SongGameDev! ;D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mouse-click movement 0 Answers

Object Movement via Mouse Click? 3 Answers

Click Goes Everywhere 0 Answers

OnMouseUp() Click Display Effect. 1 Answer

How to properly set the position clicked by the mouse? 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