- Home /
2D Mouse and Right analog as Direction for the flashlight.
I am trying to make a flashlight facing a direction pointed by analog or mouse.
Here is my code for the playercontroller:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
void Update (){
float input_x = Input.GetAxisRaw ("Horizontal");
float input_y = Input.GetAxisRaw ("Vertical");
bool isWalking = (Mathf.Abs (input_x) + Mathf.Abs (input_y)) > 0;
anim.SetBool ("isWalking", isWalking);
if (isWalking) {
anim.SetFloat ("x", input_x);
anim.SetFloat ("y", input_y);
transform.position += new Vector3 (input_x, input_y, 0).normalized * Time.deltaTime;
}
}
}
And here is my code for flashlight:
void Update () {
if (Input.GetAxis ("Horizontal") > 0) {
transform.localEulerAngles = new Vector3 (0, 70, 0);
}
if (Input.GetAxis ("Horizontal") < 0) {
transform.localEulerAngles = new Vector3 (0, 290, 0);
}
if (Input.GetAxis ("Vertical") > 0) {
transform.localEulerAngles = new Vector3 (290, 0, 0);
}
if (Input.GetAxis ("Vertical") < 0) {
transform.localEulerAngles = new Vector3 (70, 0, 0);
}
}
Answer by Bioder · Apr 21, 2015 at 01:30 PM
I think i have to make my flashlight look at mouse by this code:
Vector3 mouseScreen = Input.mousePosition;
Vector3 mouse = mainCam.ScreenToWorldPoint(mouseScreen);
transform.rotation = Quaternion.Euler(320, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90, 0);
And when its rotating Y by 10 it has to be rotating X by 10 too.
Answer by Eno-Khaon · Apr 21, 2015 at 12:25 AM
You're using the same Axis calls for your flashlight direction as you are for movement.
https://unity3d.com/learn/tutorials/modules/beginner/scripting/get-axis
That should get you pointed in the right direction. You'll need to define your own axes which make use of a right Analog Stick and Mouse inputs together.
Additionally, because you're only pointing in strict 90-degree-rotated directions (assuming that's the intent), you would also want to determine the greatest input of those with a small Dead Zone as a buffer.
If you're looking for precise aim for an alternative for mouse control, you will want to point in the direction of either the mouse cursor's position on screen (translated into a generalized world position) or the destination of a raycast from the mouse's position.
One other note: 0, 90, 180, 270. Your flashlight is currently set to face at a few odd angles, where it would be better to give it an empty parent, point the flashlight itself upward, then rotate around only the Z axis, rather than rotating it to up/down/left/right from forward. Then you can use 0, 90, 180, and 270 degree rotations instead.
Your answer
Follow this Question
Related Questions
smooth raycast LookAt ? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a torque-based sphere controller by simple mouse inputs 2 Answers
Player to look at 0 Answers
Bullet mouse control 2 Answers