• 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 LockonS · Jan 31, 2012 at 05:22 AM · inputdirectionmovingenumwalking

8 direction walking not working...

So, I was attempting to make an 8 direction walking, it doesn't work like it's supposed to. here's my code

 function Update () {
 var VeIn = Input.GetAxis("Vertical");
 var HoIn = Input.GetAxis("Horizontal");
 if(VeIn > 0 && HoIn == 0){
     Move = MoveType.Up;
 }
 else if(VeIn > 0 && HoIn > 0){
     Move = MoveType.UpRight;
 }
 else if(VeIn == 0 && HoIn > 0){
     Move = MoveType.Right;
 }
 else if(VeIn < 0 && HoIn > 0){
     Move = MoveType.RightDown;
 }
 else if(VeIn < 0 && HoIn == 0){
     Move = MoveType.Down;
 }
 else if(VeIn < 0 && HoIn < 0){
     Move = MoveType.DownLeft;
 }
 else if(VeIn == 0 && HoIn < 0){
     Move = MoveType.Left;
 }
 else if(VeIn > 0 && HoIn < 0){
     Move = MoveType.LeftUp;
 }
 else{
     Move = MoveType.None;
 }
 switch (Move){
     case MoveType.None:
         CurrentSpeed = 0;
         break;
     case MoveType.Up:
         transform.rotation.y = 45;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.UpRight:
         transform.rotation.y = 90;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.Right:
         transform.rotation.y = 135;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.RightDown:
         transform.rotation.y = 180;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.Down:
         transform.rotation.y = 225;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.DownLeft:
         transform.rotation.y = 270;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.Left:
         transform.rotation.y = 315;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     case MoveType.LeftUp:
         transform.rotation.y = 0;
         CurrentSpeed = MoveSpeed * Time.deltaTime;
         break;
     
 }
 print("Move = " + Move);
 transform.Translate(0,0,CurrentSpeed);

}

well, when i press left and up together, it works, and down and right works, everything else is just weird.....

so well, if somebody could help, i would greatly appreciate it!

--LockonS483

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

Answer by syclamoth · Jan 31, 2012 at 05:29 AM

Wouldn't it be simpler to just do this?

 var moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Horizontal"));
 transform.LookAt(moveDirection);
 if(moveDirection.magnitude > 1)
 {
     moveDirection = moveDirection.normalized;
 }
 transform.Translate(0, 0, MoveSpeed * Time.deltaTime * moveDirection.magnitude);

Your code just seems to be absurdly complicated for such a simple task. There are at least 4 problems with it just from looking- the least of which is attempting to manually assign to transform.rotation. Transform.rotation is a Quaternion- it doesn't work the way you seem to think it does.

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 syclamoth · Jan 31, 2012 at 05:31 AM 1
Share

If you don't like the way the axes get smoothed, you can fix that by using 'Input.GetAxisRaw(string)' This way, they will act as simple binary switches, ins$$anonymous$$d of faking joystick behaviour.

avatar image LockonS · Feb 01, 2012 at 12:41 AM 0
Share

so, thanks for the reply, but that does not seem to be much of a 8-direction moving to me. I am not the most experienced person in the world so, i would like to ask, how do i change an objects rotation if not by "transform.rotation.y = x"? I would greatly appreciate an answer thank you again for your reply!

--LockonS

avatar image syclamoth · Feb 01, 2012 at 01:41 AM 1
Share

Like I said, tranform.rotation.y does not do what you think it does. transform.rotation isn't stored as a 3-axis rotation, it's a 4-axis quaternion!

In any case, did you even try the code I gave you? The 'transform.LookAt(direction)' is the part that rotates the character. And it rotates in 8 directions, because that is the number of combinations of the two axes, converted into a Vector2. Just because you don't understand what a bit of code does, doesn't mean it doesn't work.

avatar image LockonS · Feb 02, 2012 at 04:07 AM 0
Share

well, thanks for your suggestion, i have fixed the problem, though. i used Waypoints that always follow the player, and turn to a specified waypoint whenever it needs to. Thanks for your help anyway, because i did not know that you could not change transform.rotation.y :P. Thank you again for your response!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

transforming player position on input.get button down 1 Answer

moving gun in first person shooter while walking 8 Answers

make object move in the same direction it faceing 1 Answer

How to use Unity Enums in a WinForms project 1 Answer

Horizontal/Vertical input immediately zero after key release 1 Answer


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