- Home /
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!
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?
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!
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;
}
}
}
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 !
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!
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.