Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by andre_95 · Apr 23, 2017 at 01:00 PM · rotationquaternioncharacter movement

Quaternion problem for character rotation

Hi, guys! I don't understand much of Unity and C#, yet! So, i could use some help from you.

The code that i implemented in my character isn't working as supposed.
I'm tryint to reproduce the same movement style as in the Super Mario game video.


Here's the code:

 // Update is called once per frame
 void Update() {
     CharacterController controller = GetComponent<CharacterController> ();

     if (controller.isGrounded) {
         moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
         moveDirection = transform.TransformDirection (moveDirection);
         moveDirection *= runningSpeed; //Speed when moving

         if (Input.GetButton ("Jump")) {                
             moveDirection.y = jumpSpeed; //1xJump
             anim.SetBool ("Jump", true);
         } else {
             anim.SetBool ("Jump", false);
         }

         //Rotation to the 8 directions
         Vector3 inputDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
         Vector3 v = inputDir.normalized;
         v.x = Mathf.Round (v.x);
         v.z = Mathf.Round (v.z);
         if (v.sqrMagnitude > 0.1f)
             targetDir = v.normalized;

         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (targetDir), Time.deltaTime * rotationSpeed);

     }
     moveDirection.y -= gravity * Time.deltaTime; // Effect of gravity
     controller.Move (moveDirection * Time.deltaTime);
 }


Here's the issue:

When i type left, my character faces left, like in the Super Mario video posted here, but while facing that new direction, my character moves towards the camera. Same happens when pressing right. And when pressing down, he faces the camera, but instead of moving towards her, he moves in the opposite direction.

Also, he doesn't move in the diagonals, while pressing Up and Right/Left simultaneously.

Console keeps saying: "Look rotation viewing vector is zero"


I hope what i just wrote seems clear to you! Thanks, in advance, for the help given!

Comment
Add comment · Show 3
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 Glurth · Apr 23, 2017 at 09:29 PM 0
Share
 moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));

This seems bit odd to me; usually, the Z component of a vector is "into/out of" the screen, and the Y components is for vertical.

 moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"),0);

This should change the "in/out" movement into an "up/down" movement.

avatar image andre_95 Glurth · Apr 24, 2017 at 09:16 AM 0
Share

In Unity, the vertical is defined as the Z axis. The alteration on code that you suggested will make the character's position dependent on the input, that means, when i press up key, the character will move in the Y axis, the same axis used for the jump.

avatar image Namey5 Glurth · Apr 24, 2017 at 10:23 AM 0
Share
 Input.GetAxis ("Vertical");

Does not mean the physical axis, it means 'axis'; as in controller input in terms of a scalar value.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Apr 24, 2017 at 10:21 AM

 moveDirection = transform.TransformDirection (moveDirection);

Remove this line. Because you are rotating the character, transforming the move direction into local space also changes the direction it's moving.

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 andre_95 · Apr 24, 2017 at 10:29 AM 0
Share

@Namey5 I just removed that line, but without it the character doesn't move. Should i remove the lines where moveDirection is mentioned too?

avatar image Namey5 andre_95 · Apr 24, 2017 at 01:50 PM 0
Share

There's no reason why the character would stop moving, are you certain you removed the correct line and nothing else? All this line does is convert it from a world space direction to a local space direction, i.e. a direction relating to the direction in which the object itself is facing. The whole Update void should be the following;

  void Update() {
      CharacterController controller = GetComponent<CharacterController> ();
      if (controller.isGrounded) {
          moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
          moveDirection *= runningSpeed; //Speed when moving
          if (Input.GetButton ("Jump")) {                
              moveDirection.y = jumpSpeed; //1xJump
              anim.SetBool ("Jump", true);
          } else {
              anim.SetBool ("Jump", false);
          }
          //Rotation to the 8 directions
          Vector3 inputDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
          Vector3 v = inputDir.normalized;
          v.x = $$anonymous$$athf.Round (v.x);
          v.z = $$anonymous$$athf.Round (v.z);
          if (v.sqr$$anonymous$$agnitude > 0.1f)
              targetDir = v.normalized;
          transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (targetDir), Time.deltaTime * rotationSpeed);
      }
      moveDirection.y -= gravity * Time.deltaTime; // Effect of gravity
      controller.$$anonymous$$ove (moveDirection * Time.deltaTime);
  }

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

115 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

Related Questions

Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers

Rotate around a point with a specific angle 0 Answers

How to make camera moving as the player on a planet 0 Answers

2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers

Rotate plane in relation to camera 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