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 Malfegor · Jan 10, 2013 at 02:20 PM · gridmouse movement

Mouse movement on grid

Hey, I can't figure this out on my own, so I thought I might ask for some help. I'm having a problem with my Mouse movement. My character walks where I want it to, but I want to make it move in a grid, with 'Move Points'. I already have a script which casts a ray from the mouse position and detects collision, but now i want to make my character move over a grid. My script: using UnityEngine; using System.Collections;

 public class MovementScript : MonoBehaviour {
 
     public bool isCollider = false;
     Vector3 movePosition;
     public float speed = 10F; 
     public Transform player; //The Player
     private float startTime;
     public float duration = 1.0F;
     public int gridSize = 1; //The size of one grid/tile
 
     void Update () {
          //The mouse click 'n walk part
          RaycastHit hit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
              if(Input.GetMouseButtonDown (0))
              {
                    movePosition = new Vector3(hit.point.x,   
                      1.360456F, hit.point.z);
           Debug.Log (hit.collider.name + " " + hit.point);
                   if (hit.collider.name != ("Tile(Clone)"))
                   {
                       isCollider = false;
               }
                     else if (hit.collider.name == ("Tile(Clone)"))
               {
                      isCollider = true;    
               }
               if (hit.collider.tag == ("Player"))
               {
                Debug.Log("Player");    
               }
              }
        }
        if (isCollider == true)
        {
              //Here the script for the actual movement
        }    
     }
 }

There might have to be a few changes in the 'movePosition = new Vector3.... etc.' In the game i'm making the player has to walk horizontal and vertical, not diagonal. I really hope someone might be able to help me.

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

Answer by $$anonymous$$ · Jan 10, 2013 at 02:57 PM

If you want the character to move like that, you can realize that the vector that represents the movement (change of position) of the character is given like this:

 moveVector = hit.point - transform.position;

Of course if you add this to transform.position, it's gonna be hit.point, effectively replacing the character. But you can also see that moveVector is a vector that's the sum of the vectors where each one has value for just one of the axes. It's like when you have a vector in a coordinate system (4,6), that is the same as (4,0) + (0, 6).

So what you do here is calculate moveVector, then in your movement code you go first on the X and then on Z (or the opposite), so you first get to the location transform.position + new Vector3(moveVector.x, 0, 0), then you go to transform.position + new Vector3(0, 0, moveVector.z), and you're there.

EDIT: I wrote a script which implements this principle with coroutines to keep the code nice and simple: **RectangularMover.cs**

alt text

Comment
Add comment · Show 8 · 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 Malfegor · Jan 10, 2013 at 03:07 PM 0
Share

Thanks, that really helped me out ^_^

avatar image Malfegor · Jan 10, 2013 at 03:26 PM 0
Share

One little question, though: Right now I have it defined like this

moveVector = hit.point - transform.position; movePosition = transform.position + new Vector3 (moveVector.x, 0, 0);

And in the 'if (isCollider == true)' statement this

transform.position = Vector3.$$anonymous$$oveTowards(transform.position, movePosition.x, speed * Time.smoothDeltaTime);

I've got the feeling I've done something wrong there... I also get the error: Best overloaded method $$anonymous$$atch for the Vector3.$$anonymous$$oveTowards and I think that is because I gave it just a single position and not a whole vector to move to. I don't know how to define the single x-z movement otherwise...

avatar image $$anonymous$$ · Jan 10, 2013 at 03:40 PM 1
Share

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.$$anonymous$$oveTowards.html

You don't need to reference the x of movePosition because you only added moveVector.x to the position. So this will move your character only on the x axis:

transform.position = Vector3.$$anonymous$$oveTowards(transform.position, movePosition, speed * Time.smoothDeltaTime);

After you get there, you want to update movePosition like this:

movePosition = transform.position + new Vector3(0,0,moveVector.z);

Then you move again.

avatar image $$anonymous$$ · Jan 10, 2013 at 03:54 PM 1
Share

You're welcome.

avatar image $$anonymous$$ · Jan 10, 2013 at 09:23 PM 1
Share

I added a link to the answer to a script I wrote. It does exactly what it should, but I used different variable names in it, and also tags ins$$anonymous$$d of names. Try to absorb it!

Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Sync Animation To Music or Bpm ? 2 Answers

Grid Letter Box is not expanding, 0 Answers

How to Place and arrange 3D cards using Grid and Bounds 0 Answers

How to name each grid cell in a grid... 0 Answers

Unity2D Grid disappears when zooming out 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