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 Bioder · Apr 19, 2015 at 11:03 PM · mousecontrollerlookatflashlightfacing

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.

alt text

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);    
         }
     }

flashlight.jpg (8.7 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

2 Replies

· Add your reply
  • Sort: 
avatar image
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.

Comment
Add comment · 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
0

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.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

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


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