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 Humblebee08 · Jan 02, 2021 at 10:39 PM · inputcontrollercamera rotate

Thumbstick camera rotation is slower when rotating in a diagonal direction

Hi, sorry if this is a simple fix. I'm trying to set up my camera rotation script with a controller, the problem is, the camera rotates slower the more diagonal the input is. This makes it difficult to track moving targets when the input isn't consistent. Is there a way to remap the input so that the input is always consistent even at diagonal angles? I've tried to illustrate the issue and what I want below.

alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class CameraLookController : MonoBehaviour
 {
     public PlayerInput playerInput;
 
     [SerializeField] private Transform playerBody;
     [SerializeField] private Transform playerCamera;
     [SerializeField] private float lookSensitivity;
     [SerializeField] private AnimationCurve inputCurve;
     [SerializeField] private float clampAngle = 90f;
 
     private Vector2 inputDirection;
     private float clampedRotationX;
 
     private void Awake()
     {
         playerInput = new PlayerInput();
         playerInput.Player.Look.performed += context => GetLookInput(context.ReadValue<Vector2>());
         playerInput.Player.Look.canceled += context => GetLookInput(context.ReadValue<Vector2>());
 
         inputCurve.preWrapMode = WrapMode.PingPong;
     }
 
     private void OnEnable()
     {
         playerInput.Enable();
 
         Cursor.visible = false;
     }
 
     private void OnDisable()
     {
         playerInput.Disable();
     }
 
     private void GetLookInput(Vector2 axis)
     {
         inputDirection.x = axis.x;
         inputDirection.y = axis.y;
 
         print("Input Direction " + inputDirection);
     }
 
     private void Update()
     {
         //Camera Look
         int xDirection;
         int yDirection;
 
         xDirection = inputDirection.x > 0 ?  1 : -1;
         yDirection = inputDirection.y > 0 ?  1 : -1;
 
         float curveEvaluatedX = inputCurve.Evaluate(inputDirection.x) * xDirection;
         float curveEvaluatedY = inputCurve.Evaluate(inputDirection.y) * yDirection;
 
         float modifiedInputX = curveEvaluatedX * lookSensitivity * Time.deltaTime;
         float modifiedInputY = curveEvaluatedY * lookSensitivity * Time.deltaTime;
 
         clampedRotationX -= modifiedInputY;
         clampedRotationX = Mathf.Clamp(clampedRotationX, -clampAngle, clampAngle);
 
         playerCamera.localRotation = Quaternion.Euler(clampedRotationX, 0f, 0f);
         playerBody.Rotate(Vector3.up * modifiedInputX);
     }
 
 }
 
web-1920-2.png (205.8 kB)
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 Namey5 · Jan 04, 2021 at 10:40 AM

To do this you would want to 'un-normalise' the input vector - there are more mathematically 'correct' ways of doing this (using actual trig functions), but we can also just abuse some trigonometric relationships to get to that point;

 private void GetLookInput(Vector2 axis)
 {
     //Normalising the vector will return a point on the circumference of a unit circle in the same direction as the vector
     Vector2 dir = axis.normalized;
     //We can then abuse the properties of this circle mapping to remap the points onto the edge of a unit square (basically just scaling the circular vector until it hits an edge of the square) and finally scale our original vector by this same weight
     inputDirection = axis / Mathf.Max (Mathf.Abs (dir.x), Mathf.Abs (dir.y), Mathf.Epsilon);
 }

Untested, but to my tired brain that looks about right.

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 Humblebee08 · Jan 10, 2021 at 10:38 PM 0
Share

That was exactly what I was looking for, Thank you!

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

145 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

Related Questions

Virtual joystick in the new input system 0 Answers

When I try to bind a key in the new input system bind dropdown, nothing shows (linux) 2 Answers

Multiple x360 controllers get wrong IDs assigned. 2 Answers

Detect if an XBOX 360 controller is plugged in 1 Answer

My Players speed differs depending on the input method (gamepad or keyboard) 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