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 Sirm · Nov 11, 2016 at 08:32 AM · controllermouselook

Using both Mouse Aim and Controller Aim at the same time

ok so, so far i have two scripts that control the direction and rotation that the object is facing, on for mouse aim and one for using the right analog stick. they both work perfectly as intended by themselves but i cannot think of a way to get it to switch between using the mouse input and using the controller input

     var mouse_pos : Vector3;
     var target : Transform;
     var object_pos : Vector3;
     var angle : float;
     
     function Update() {
         
 //for right analog aiming
         joyx_pos = Input.GetAxis("RHorizontal");
         joyy_pos = Input.GetAxis("RVertical");
         angle = Mathf.Atan2(joyy_pos, joyx_pos) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(Vector3(0,0,angle));
         
 //for mouse aiming    
         mouse_pos = Input.mousePosition;
         mouse_pos.z = 5.23;
         object_pos = Camera.main.WorldToScreenPoint(target.position);
         mouse_pos.x = mouse_pos.x - object_pos.x;
         mouse_pos.y = mouse_pos.y - object_pos.y;
         angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
         
     }

kinda new here but any help or ideas would be appreciated

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 Sirm · Nov 18, 2016 at 01:43 AM

ok so i managed to figure it out myself

     var target : Transform;
     var object_pos : Vector3;
     var angle : float;
     var inputselection : boolean; //false = controller, true = mouse
 
 function Update() {
     

     
     if(Input.GetAxis("Mouse X")<0 || Input.GetAxis("Mouse X")>0 || Input.GetAxis("Mouse Y")<0 || Input.GetAxis("Mouse Y")>0){
     inputselection = true;
     }
     if(Input.GetAxis("RHorizontal")<0 || Input.GetAxis("RHorizontal")>0 || Input.GetAxis("RVertical")<0 || Input.GetAxis("RVertical")>0){
     inputselection = false;
     }
     
     if(!inputselection){
     joyx_pos = Input.GetAxis("RHorizontal");
     joyy_pos = Input.GetAxis("RVertical");
     angle = Mathf.Atan2(joyy_pos, joyx_pos) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(Vector3(0,0,angle));
     }
     
     if(inputselection){
     mouse_pos = Input.mousePosition;
     mouse_pos.z = 10;
     object_pos = Camera.main.WorldToScreenPoint(target.position);
     mouse_pos.x = mouse_pos.x - object_pos.x;
     mouse_pos.y = mouse_pos.y - object_pos.y;
     angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
     }
 }`

using the boolean inputselection, i could determine which input type was being used the second it detected input from either the mouse or the controllers second analog stick, and because the aiming and the movement are completely separate, you can use the controller for movement and the mouse for aiming or whatever combination the player chooses without having to change any settings

the first 2 if statements determine if the mouse is moving or not and if there is any input from the right analog stick, and switching between the rotation controls based on the last input detected

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 ethanicus · Apr 10, 2019 at 04:13 PM 1
Share

I know this was a long time ago, but for anybody co$$anonymous$$g here later, it might be better to use an enum ins$$anonymous$$d of a bool for the input type. That way you can add more than two types if needed later on, and it would be named ins$$anonymous$$d of having to remember what true and false mean.

avatar image zodianderxtrelian · Jun 08, 2020 at 07:25 PM 0
Share

Thank you. This really helped me. Was trying to solve it for such a long time.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

use controller to look, but still have mouse to click buttons 1 Answer

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Controlling MouseLook script with keys/joystick 1 Answer

Character Controller help 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