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 pickle chips · Jun 19, 2013 at 01:48 AM · movementtutorialunderstandexplanation

Could someone explain this code for me please?

So I'm following burgzergarcade's hack and slash rpg tutorial, and for moving the character we used this code:

(These are the variables)

 public CharacterController controller; //This is assigned to the controller elsewhere in the script
 private Vector3 moveDirection = Vector3.zero;
 private CollisionFlags collisionFlags;

Here is the code which gets it to move

         if (controller.isGrounded) {
             moveDirection = new Vector3 (0, 0, Input.GetAxis("Vertical"));    
             moveDirection = transform.TransformDirection(moveDirection).normalized;
             moveDirection *= moveSpeed * Time.deltaTime;
         }
         
         collisionFlags = controller.Move(moveDirection);
     }

I didn't really understand the brief description given in the tutorial, and I looked up all the functions (collisionFlags and TransformDirection)in the unity script Reference but I still don't really understand how this works. (I understand IsGrounded and Normalized, just not the rest) The code works, but I just don't understand why it works. Prior to the tutorial I just used this to go forward, and the opposite to move backwards:

 if (Input.GetAxis("Vertical") > 0) {
     controller.Move(transform.forward * moveSpeed * Time.deltatime);
 }

The results seem to be the same, So would someone be able to explain how this (new) code gets my character to move, and the difference between it and my old code?

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 · Jun 19, 2013 at 03:18 AM

In order to understand this code, you first have to understand the different between local and world coordinates. Local coordinates are relative to the object. The origin of those coordinates are at the pivot point of the object. Relative to that coordinates system, 'forward', 'back,' 'up,' 'down,' 'left,' 'right' always remain constant. For example, the local 'forward' is always (0,0,1) which is also Vector3.forward. 'Up' is always (0,1,0) which is also Vector3.up. It doesn't matter what way the object is facing since the coordinates are always relative the object.

World coordinates are based world axes. We can get our 'forward' vector in world space from the transform using Transform.forward. Likewise 'up' is Transform.up, and 'right' is Transform.right. To can translate from local to world using Transform.TransformDirection(). So these two things are equivalent:

 transform.forward == Transform.TransformDirection(Vector3.forward)

So the new code you don't understand constructs a vector forward in the local coordinated system. Then it translates it to the global coordinate system. For what you are doing here, I cannot any benefit to the new code, and the old code is simpler. The new code might be simpler if you were combining forward/back with left/right movement:

 moveDirection = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
Comment
Add comment · Show 3 · 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 pickle chips · Jun 19, 2013 at 07:40 PM 0
Share

Ohhh okay I think I understand it now. Thanks! So basically the transform.TransformDirection line is just converting the world forward to the local forward? and also what does the collisionFlags do? How does simply assigning the collisionFlags variable to controller.$$anonymous$$ove call the controller.$$anonymous$$ove Function?

Thanks for your answer though! I understand the main gist of it now :)

avatar image robertbu · Jun 20, 2013 at 12:02 AM 0
Share

You have it backwards. '`transform.TransformDirection(`)' translates local coordinates to world coordinates. The collision flags are a return value. They specify what side(s) of the character controller collided in the move. They are not necessary in the code you've shown me here. That is you could just do:

 controller.$$anonymous$$ove(moveDirection);

It may be that some code further down that you did not past into the question uses these flags for some reason, but they are not necessary to get the controller to move.

avatar image pickle chips · Jun 20, 2013 at 05:36 PM 0
Share

Oh okay i understand now, Thanks!

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

14 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

Related Questions

Can't change the speed of a character. 2 Answers

Z axis change 2 Answers

Really want to get into Unity but dont understand coding 1 Answer

help with movement along x axis and slow down. please help 1 Answer

Problem with script 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