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 DubstepDragon · Jan 03, 2014 at 08:17 PM · movementgridgridmovebased

Faults with Grid-Based movement...

I have a few uncertainties here. I basically achieved the basics of moving the object by 1 when one of the directional keys are pressed, and it works fine. However, I also wanted the "Q" (left) and "E" (right) keys to rotate the object 90° to their direction. This is my script, I was unsure how to do the rotation, even after an hour on the Unity Script Reference.

 using UnityEngine;
 using System.Collections;
 
 public class movement : MonoBehaviour {
     public float speed = 2.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;
         }
         
         if(!moving && Input.GetKey(KeyCode.A)){
             moving = true;
             endpos = transform.position + Vector3.left;
         }
         
         if(!moving && Input.GetKey(KeyCode.S)){
             moving = true;
             endpos = transform.position + Vector3.back;
         }
         
         if(!moving && Input.GetKey(KeyCode.D)){
             moving = true;
             endpos = transform.position + Vector3.right;
         }
         
         if(!moving && Input.GetKey(KeyCode.Q)){
             moving = true;
             endpos = UNSURE WHAT TO PLACE HERE;
         }
         
         if(!moving && Input.GetKey(KeyCode.E)){
             moving = true;
             endpos = UNSURE WHAT TO PLACE HERE;
         }
         
         transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
     }
 }

Thanks in advance! :D

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

Answer by sparkzbarca · Jan 03, 2014 at 08:23 PM

transform.rotate(0,90,0);

Comment
Add comment · Show 4 · 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 DubstepDragon · Jan 03, 2014 at 08:27 PM 0
Share

It returns the error:

 Assets/movement.cs(40,44): error CS1061: Type `UnityEngine.Transform' does not contain a definition for `rotate' and no extension method `rotate' of type `UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)


Also, no matter how much I increase the speed, the movement speed does not increase...

avatar image DubstepDragon · Jan 03, 2014 at 08:38 PM 0
Share

Also, how do I get it to use local transform ins$$anonymous$$d of global? So as to move forward even though the object was rotated.

avatar image sparkzbarca · Jan 03, 2014 at 08:40 PM 0
Share

technically its transform.Rotate I just assumed you'd get the autocomplete

the local is

transform.forward;

global is

vector3.forward

avatar image DubstepDragon · Jan 03, 2014 at 08:50 PM 0
Share

Thanks very much! Oh and my bad, sorry :P

avatar image
1

Answer by HappyMoo · Jan 03, 2014 at 09:11 PM

  • Your test (transform.position == endpos) is dangerous... it works for now, because MoveTowards can hit the target exactly, but be aware that if you use something else later, it could be that you animate the position really close to the target, but never hit it.

  • The speed parameter works to speed things up, but you probably change it in code and it stays the same in the inspector... change it in the inspector

  • to move the way you want, use relative direction, e.g. endpos = transform.position + transform.forward; Watch out, there is no left/back, you need to use -right and -forward

  • for rotating, do something like: transform.Rotate(Vector3.up, 90) and Input.GetKeyDown(KeyCode.E) or you will rotate every 1/60 second

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 DubstepDragon · Jan 03, 2014 at 10:00 PM 1
Share

Thank you so much :D Sorry your answer is perfect, however I cannot mark it as correct because I already got my solution from the previous one... But thanks! I used your rotation method and now I finally know what was wrong with the speed :D

avatar image HappyMoo · Jan 03, 2014 at 10:04 PM 0
Share

Great I could help you out.

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

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

Related Questions

Adding collision detection to movement script... 1 Answer

Manual Collision Detection through triggers... 0 Answers

A node in a childnode? 1 Answer

Tell an object that a position in grid is occupied 0 Answers

how to make objects move in rows and columns? 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