- Home /
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
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
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.
Thank you. This really helped me. Was trying to solve it for such a long time.
Your answer

Follow this Question
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