• 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
1
Question by dddaw · May 15 at 04:45 PM · rotation3dscripting beginneraxisnegative

how to only get the negative or positive part of a GetAxis

I am trying to make a top-down game like Metal Gear Solid 1 and I want my player charter to look at the way he is moving but all the ways I have found online don't work for me so I am trying to make my own here is what I have so far.

  ` moveimput.x = Input.GetAxisRaw("Horizontal");
     moveimput.z = Input.GetAxisRaw("Vertical");

     transform.localEulerAngles = new Vector3(0, 0 * -moveimput.z + moveimput.x * 90, 0);`

I don't know if I am just stupid and the solution is obvious but I need help.

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 Thygrrr · May 15 at 05:10 PM

Use Quaternion.LookRotation to generate a Rotation Quaternion you can assign to your transform. You can also use the Vector3.right and .forward constants to build a matching vector a little more conveniently than trying to build a vector from scratch like you did (which can get confusing - you're not stupid :). You tried to do it with Euler Angles, that's a little harder and generally Quaternions are always preferable. With the Eulers, you could use Vector3.SignedAngle(from, to) to get your angle. I advise against it. :)

Recommended way:

 using UnityEngine;
 
 public class SimpleLookRotation : MonoBehaviour
 {
     private void Update()
     {
         // Calculate World-Space Standard Coordinate direction from the input.
         var inputDirection = Input.GetAxis("Vertical") * Vector3.forward + 
                              Input.GetAxis("Horizontal") * Vector3.right;
 
         //If user isn't actively looking with the controller, abort.
         if (inputDirection.magnitude < 0.1f) return;
         
         //Generate rotation and apply it to the transform.
         transform.rotation = Quaternion.LookRotation(inputDirection);
     }
 }
 

This is the BAD way:

 using UnityEngine;
 
 public class SimpleLookRotation : MonoBehaviour
 {
     private void Update()
     {
         // Calculate World-Space Standard Coordinate direction from the input.
         var inputDirection = Input.GetAxis("Vertical") * Vector3.forward + 
                              Input.GetAxis("Horizontal") * Vector3.right;
 
         //If user isn't actively looking with the controller, abort.
         if (inputDirection.magnitude < 0.1f) return;
 
         //This is not the best way to do it, but to get the angle from a direction, use Vector3.SignedAngle (Mathf.Atan2 is of limited use)
         var eulerAngles = new Vector3(0, Vector3.SignedAngle(Vector3.forward, inputDirection, Vector3.up), 0);
         
         //Generate rotation and apply it to the transform.
         transform.eulerAngles = eulerAngles;
     }
 }
 


Protip: You probably want to smooth out your rotation a little, for a start you can use this (use a number > 0 and <= 1 to smooth it, higher numbers turn faster):

 //Generate rotation, smooth it from the previous, and apply it to the transform.
 var softRotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDirection), 0.2f);
 transform.rotation = softRotation;
 
 


Comment
Add comment · Show 1 · 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 dddaw · May 15 at 05:36 PM 1
Share

thank you after implementing this it is working perfectly.

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

233 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 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

LookAt() with X axis 1 Answer

¿Is it possible to scale independently of the rotation? 0 Answers

How to lock a transform's local up axis rotation on any surface 1 Answer

Trouble Getting My Player Object's Script to interact with My stage's Script 1 Answer

Input.acceleration Y and Z are the same? 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