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