Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 deneme09 · Jun 15, 2018 at 05:25 AM · movementvector3directioncharacter controller

How can I move character controller in one direction

Hi guys. I want to move my character controller in one direction, but I can't.

I can move my character in all axis with this code;

 float hAxis = Input.GetAxis("Horizontal");
 float vAxis = Input.GetAxis("Vertical");
 public Camera cam;
 Vector3 scale = new Vector3(1, 0, 1);
 Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
 Vector3 camRight = Vector3.Scale(cam.transform.right, scale).normalized;
 Vector3 vertical = camForward * vAxis;
 Vector3 horizontal = camRight * hAxis;
 Vector3 move = (vertical + horizontal);

I can move my character in only X axis with this code;

 Vector3 scale = new Vector3(1, 0, 0); // I set X axis to 1, others to 0. So character controller is moving in only X axis.
 Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
 Vector3 camRight = Vector3.Scale(cam.transform.right, scale).normalized;
 Vector3 vertical = camForward * vAxis;
 Vector3 horizontal = camRight * hAxis;
 Vector3 move = (vertical + horizontal);

I can move my character in only Z axis with this code;

 Vector3 scale = new Vector3(0, 0, 1); // I set Z axis to 1, others to 0. So character controller is moving in only Z axis.
 Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
 Vector3 camRight = Vector3.Scale(cam.transform.right, scale).normalized;
 Vector3 vertical = camForward * vAxis;
 Vector3 horizontal = camRight * hAxis;
 Vector3 move = (vertical + horizontal);

But I can't move character in only a direction, b direction or c direction. (Look at the image, you can see, a b c directions) How can I move my character in only a direction, b direction or c direction? alt text Thank you very much guys...

axisimage.png (15.5 kB)
Comment
Add comment · Show 1
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 Harinezumi · Jun 15, 2018 at 08:49 AM 0
Share

First, some optimizations: Vector3(1, 0, 0) is the same as Vector3.right, Vector3(0, 1, 0) is the same as Vector3.up, and Vector3(0, 0, 1) is the same as Vector3.forward. These are NOT the same as transform.forward/up/right, because those depend on the rotation of the transform.
The code that uses Vector3.Scale() can also be simplified to just excluding the coordinate you don't want to use, e.g. if Vector3 scale = new Vector3(1, 0, 0); then ins$$anonymous$$d of Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized; you can write Vector3 camForward = new Vector3(cam.transform.forward, 0, 0).normalized;. This saves you 2 unnecessary multiplications. Unless, of course, scale will be very variable, then the original code is more general, and better.

Now for your actual question: what are the directions a, b, and c? Are they at 45 degrees between the axes? Anyway, the code should work if you set scale to the direction (axis) along which you want to move and normalize it (if the coordinates are not equal). What are you seeing that makes you say it doesn't work?

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

"Fake" Vector3.forward? 3 Answers

Get which way the player is moving from character controller.move 2 Answers

Fire Knockback, Vectors, Directions 1 Answer

How to find the direction to translate an object, to move towards a point, regardless of rotation? 1 Answer

Calculate direction of travel. 3 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