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 Recino · Feb 14, 2014 at 04:48 AM · cameracamera-movementlookatnoob

How do you force a player to look at something?

Okay so I've been trying for class to make a script that will make the player look away from what they are looking at to look at another target and then look back. I looked into quanternians, Euler angles, slerp, and lerp, but I don't get it. I just want to be able to force the player to look where I want them to look so I can change some things while they aren't looking and then return camera control to them. I know this is a scripting issue. I already can turn off their mouse look (in C#), but I don't know where to go from there.

I'm not even sure if I should ask this on unity answers or not, but I don't quite know what else to do. Thanks in advance for any help you guys can give!

Comment
Add comment · Show 2
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 Owen-Reynolds · Feb 14, 2014 at 07:22 PM 0
Share

Is there a $$anonymous$$cher (vs. Independent Study?) What kind of class is it? In theory you've been learning something which can do this. Or at least can give a clue. Like the answer involving animation -- are you animators? Or is it a program$$anonymous$$g class that happens to use Unity?

avatar image Recino · Feb 19, 2014 at 04:31 PM 0
Share

Yes there is a $$anonymous$$cher, but he's no good at progra$$anonymous$$g, so he sorta left me on my own to figure out how to do what we needed for the project we're doing.

Thank you everyone who responded, it was very helpful!

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Feb 14, 2014 at 05:16 AM

Assuming you are talking about a 3D game played on the XZ plane with 'Y' as up, here is a bit of example code to get you started. Test it in a new scene to start. After attaching this script, create two target objects. Then select the game object with this script, set the size of 'targets' to 2, and drag and drop the two target objects into the slots. This script changes where it looks when you hit the 'A' key. Based on your description, you will be doing something different, but this is example code to get you started.

 using UnityEngine;
 using System.Collections;
 
 public class LookAway : MonoBehaviour {
 
     public Transform[] targets;
     public float rotateSpeed = 90;
 
     private int currTarget = 0;
     
     void Update () {
         Vector3 lookDir = targets[currTarget].position - transform.position;
         Quaternion q = Quaternion.LookRotation(lookDir);
         transform.rotation = Quaternion.RotateTowards(transform.rotation, q, Time.deltaTime * rotateSpeed);
 
         if (Input.GetKeyDown (KeyCode.A)) {
             currTarget = (currTarget + 1) % targets.Length;
         }
     }
 }
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 Recino · Feb 19, 2014 at 04:28 PM 0
Share

Thank you! This worked best for my purposes.

avatar image
0

Answer by rangapraveen · Feb 14, 2014 at 05:31 AM

There is a prebuilt function or property given by unity -> LookAt

here is the link to know more about it : http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html

but the short version is :

 transform.LookAt(target);

hope this helps , its pretty simple and might work in your case !

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 FreeTimeDev · Feb 14, 2014 at 05:11 AM

You know the code you use to move the camera with the mouse?

When you turn off the mouse movement start another coroutine that "locks" the camera in a different direction.

http://docs.unity3d.com/Documentation/ScriptReference/Transform-eulerAngles.html

It'd be something like, Camera.main.transform.eulerAngles = new Vector3(0,0,90);

However, that would be somewhat jolting. So, when you have something that needs to change over time you probably want it in the update function, right?

When you turn off your mouse save the camera's transform.rotation. Then,use a bool to "turn on" the dramatic attention grabbing thing immediately after that. After that, in the update, check if the bool's condition is right so that you should slowly update your saved rotation to your target rotation.

     private Vector3 oldLook; //Original rotation
     private Vector3 targetLook; //where we want to look
     private bool    cantLook = false;  //Player starts out able to look
     public Transform myCam;  //drag the camera to this in inspector
     
     void Update()
     {
     
     //Magic happens here, and you can't look around any more (You said you could
     //figure that out, so, figure that part out)
 cantLook = true;
 if(cantLook == true)
 {
 //Oh My, the player loses control!
 myCam.transform.Rotate(Time.deltaTime, 0, 0);  
 //You have to figure out what speed you want
 StartCoroutine("CamTimer");
 }
 
 }
 
 IEnumerator CamTimer()
 {
 yield return new WaitForSeconds(2.0f);
 cantLook = false; //We stop moving the camera
 yield return new WaitForSeconds(1.0f);
 //Give player back control with mouse here
 }

I used transform.rotate because we don't know what rotation your camera will be at, and if you "add" say, 45 degrees to 350 degrees Unity doesn't appreciate that. I was sort of scattered here, but hopefully you followed the process and thinking. Also, none of the code was tested what so ever. So debug away!

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 GraviterX · Feb 14, 2014 at 05:16 AM

Hi. To do this, I recommend using an animation play at a certain time where it turns them to look. Then, disable the mouse look script and then enable it when you want the looking to stop.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use lookAt and transform.translate at the same time 1 Answer

RTS camera 1 Answer

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

TPS camera in multiplayer... 2 Answers

How to rig a cutscene Camera back to it's original place 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