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
1
Question by ExpiredIndexCard · Apr 02, 2013 at 02:28 AM · c#rotationscreenlight

Rotate a camera when my flashlight is on the edge of the screen... Video included

http://www.youtube.com/watch?v=FgHSBbrymkw

As you can see in the video, I have limited my flashlight so that it does not go off-screen. What i want to do with this is when my flashlight reaches the edge, I want the player to rotate as if he was looking. Here are my scripts if you need them: My flashlight Script:

using UnityEngine; using System.Collections;

public class FlashlightTurn : MonoBehaviour {

 public float lookSensitivity;
 private float yRotation;
 private float xRotation;
 private float currentYRotation;
 private float currentXRotation;
 private float yRotationV;
 private float xRotationV;
 public float lookSmoothDamp;

 private Quaternion lookForward;
 public float lookSpeed;

 public Transform camera;

 private Vector3 lookLeftLimit;


 void Start () {

     lookForward = transform.rotation;
     lookLeftLimit = new Vector3(0,130,0);
 }
 
 void Update () {
     if (!Input.anyKey)
     {
         yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
         xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;

         xRotation = Mathf.Clamp(xRotation, -30, 30);
         yRotation = Mathf.Clamp(yRotation, -50, 30);

         currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);

         transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
         

     }
     if(Input.GetKey("w") || Input.GetKey("s") || Input.GetKey("a") || Input.GetKey("d"))
         transform.localRotation = Quaternion.Lerp(transform.localRotation, lookForward, lookSpeed * Time.deltaTime);

     if (transform.localEulerAngles == lookLeftLimit) {
         xRotation += Input.GetAxis("Mouse Y") * lookSensitivity;
         transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
     }
         
 
 }

}

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
1
Best Answer

Answer by ByteSheep · Apr 02, 2013 at 02:41 AM

You should be able to just check if the yRotation variable is smaller or greater than a certain min or max value and depending on that rotate the camera e.g.:

 if(yRotation < minRotation)
 {
   Debug.Log("Turn Left");
   cam.transform.Rotate(-Vector3.up * Time.deltaTime * lookSensitivity);
 }
 else if(yRotation > maxRotation)
 {
   Debug.Log("Turn Right");
   cam.transform.Rotate(Vector3.up * Time.deltaTime * lookSensitivity);
 }
Comment
Add comment · Show 5 · 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 ExpiredIndexCard · Apr 02, 2013 at 02:45 AM 1
Share

Doesnt work. Does not rotate

avatar image ByteSheep · Apr 02, 2013 at 02:48 AM 1
Share

I have added a debug line to the code. It should say turn left / right when you rotate the flashlight.
The camera may not be rotating because you haven't got a proper reference to it.
Also make sure you paste the code on line 37 in your script.

avatar image ExpiredIndexCard · Apr 02, 2013 at 02:54 AM 1
Share

Hmmm... it does say "rotate left/right" but there is no rotation still. When i rotate the flashlight to the left, the rotation says (0, 310, 0) and when its on the right, it says (0, 30, 0). Im not sure if its the local rotation or not

avatar image ByteSheep · Apr 02, 2013 at 03:01 AM 1
Share

Well what you could do is add a reference to the camera object through a variable:

 public Transform cam;

or alternatevely you could use Chronos-L's approach and rather than getting the values of the rotation of the flashlight, get the position of the mouse on the screen.
However if you want to test it using the code above all you'd have to do is:

  • Add the line of code to the variable section of your code

  • Change "Camera.main" in my answer to "cam"

  • And drag the camera to the cam variable in the inspector

avatar image ByteSheep · Apr 02, 2013 at 03:26 AM 0
Share

Well glad it worked in the end, updated my answer.
Yeah I totally overlooked that the rotation would only be 1 degree per second..

avatar image
2

Answer by Chronos-L · Apr 02, 2013 at 02:52 AM

You can use the function I wrote in this question: http://answers.unity3d.com/questions/425712/how-can-i-move-the-camera-when-the-mouse-reaches-t.html

The function will return a Vector2, the x denotes how close the mouse is to the left-right edge while the y denotes the top-bottom edge.

You can do something like this:

 //50 is the margin
 //The larger the value, the closer it is to the edge
 Vector edge = MouseScreenEdge( 50 ); 
 
 if( edge.x < 0 ) { //Left
     //Rotate the camera here
 
     //You can use the edge.x to control how far
     //you want to rotate the camera
 }
 else if( edge.x > 0 ){ //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 ExpiredIndexCard · Apr 02, 2013 at 03:26 AM 1
Share

Thanks so much for your quick response but I did not want to add another function into my script because I had a lot. Dont want to slow down the game but this response it very good as well. Thanks!

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

11 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How do I initialize a simple light in C#? 2 Answers

My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer

Multiple Cars not working 1 Answer

[C#]Angle of shooting equale to mouse direction 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