- Home /
Camera not rotating
Hey guys! Creeperbot65 here!
I'm having an error with rotating my cut scene camera and it just won't rotate at all no matter what I do! Same thing goes for the door opening animation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StartingScene : MonoBehaviour {
public Text DialougeBox;
private double Timer;
public AudioClip FootstepSound1;
public AudioClip FootstepSound2;
public AudioClip FootstepSound3;
public AudioClip FootstepSound4;
public AudioClip DoorOpen;
public Transform Player;
private int RandomFootStep;
public GameObject Darkness;
public Transform Door;
public bool StartingSceneEnabled;
void Awake () {
StartingSceneEnabled = true;
DialougeBox.text = "";
}
void Update () {
//Increaces the time in seconds for dialouge
if (Time.time > Timer && Timer < 26) {
if (Timer != 24) {
Timer = Mathf.Round (Time.time + 1);
}
if (tag != "Player") {
if (Timer < 21) {
RandomFootStep = Random.Range (1, 4);
if (RandomFootStep == 1) {
AudioSource.PlayClipAtPoint (FootstepSound1, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
} else if (RandomFootStep == 2) {
AudioSource.PlayClipAtPoint (FootstepSound2, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
} else if (RandomFootStep == 3) {
AudioSource.PlayClipAtPoint (FootstepSound3, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
} else if (RandomFootStep == 4) {
AudioSource.PlayClipAtPoint (FootstepSound4, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
}
}
}
} else if (Timer > 23) {
if (tag != "Player") {
DialougeBox.text = "";
if (Timer == 24) {
AudioSource.PlayClipAtPoint (DoorOpen, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
for (int i = 0; i > 900; i++) {
//Error is here!
Door.Rotate (0, 0.10f, 0);
Door.position = new Vector3 (Door.position.x + 0.00074444444f, Door.position.y, Door.position.z + 0.00063333333f);
}
Timer = 25;
}
if (Timer == 27) {
Timer = 28;
GameObject MusicBGGameobject = GameObject.Find ("FPSController");
BGMusic BGMusicScript = MusicBGGameobject.GetComponent<BGMusic> ();
BGMusicScript.MusicSel = 2;
BGMusicScript.PlayMusic = true;
Instantiate (Darkness, new Vector3 (-0.2f, 1.09f, 8.28f), new Quaternion (0, 0, 0, 0));
StartingSceneEnabled = false;
}
}
}
//The starting texts
if (tag != "Player") {
if (Timer == 2) {
DialougeBox.text = "Gees, My friends are such a******s.";
} else if (Timer == 6) {
DialougeBox.text = "Forcin' me to go down here or else they'll burn my house down.";
} else if (Timer == 12) {
DialougeBox.text = "";
} else if (Timer == 15) {
DialougeBox.text = "They certainly don't sound like friends now.";
} else if (Timer == 19) {
DialougeBox.text = "";
} else if (Timer == 23) {
//Error is here!
float tiltAroundY = Player.rotation.y;
float tiltAroundX = 38.29f;
float tiltAroundZ = Player.rotation.z;
Quaternion target = Quaternion.Euler (tiltAroundX, tiltAroundY, tiltAroundZ);
Player.rotation = Quaternion.Slerp (Player.rotation, target, 0.1f);
DialougeBox.text = "I'm here...";
}
}
//Scripts for moving player
if (Timer < 19) {
Player.transform.position = new Vector3 (Player.transform.position.x, Player.transform.position.y - 0.01f, Player.transform.position.z + 0.01f);
}
}
}
Thanks!
-creeperbot65
What is the camera attached to? It looks like the transform named Player is that right?
It's worth mentioning that your lines
float tiltAroundY = Player.rotation.y;
float tiltAroundX = 38.29f;
float tiltAroundZ = Player.rotation.z;
Are storing Quaternion components as Euler components. Was this intentional? In any event, your problem might be:
Timer = $$anonymous$$athf.Round (Time.time + 1);
Your rotation block is only runs if Timer == 23, but if Timer != 24 then it gets incremented. Quaternion.Slerp() is a smooth linear interpolation that works as you'd expect over a given amount of time, but in your case it's only getting one frame to do the rotation. Have you considered adding the rotation to a coroutine?
Try doing the rotation in a coroutine:
private IEnumerator LookDownRoutine (float angle = 38.29f, float tolerance = 0.01f)
{
// Set up the angle we intend to use and adjust the x component
Vector3 lookAngle = this.transform.rotation.eulerAngles;
WaitForEndOfFrame wait = new WaitForEndOfFrame ();
// Check how close we are to the desired rotation
while ($$anonymous$$athf.Abs (this.transform.rotation.eulerAngles.x - angle) > tolerance) {
// Convert back to a Quaternion and Slerp
lookAngle.x = angle;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.Euler (lookAngle), 0.1f);
// Then wait a frame
yield return wait;
}
}
Your block would then become:
} else if (Timer == 23) {
// Use a coroutine here to make sure it works properly over time.
this.StartCoroutine (this.LookDownRoutine ());
DialougeBox.text = "I'm here...";
}
}
Could you do it without using this. I'm using the one script for multiple things at once. (I don't know if it would work because I don't understand coroutines)
"this" is a C# convention and means you're referring to the class instance attached to a gameobject. It will work on whatever you attach it to.
Typo in line 54:
for (int i = 0; i > 900; i++)
should be
for (int i = 0; i < 900; i++)
Answer by Milanese · Jan 11, 2018 at 03:29 PM
Hey! I'm not sure if rotting inside a for loop is the best choice. The for loops through before continuing with the next lines of code.
I would put the roation in an update with a counter. So you just have an int i += Time.deltaTime * (your value for speed). then I would put that int as the rotation angle of the axis that you need.
Void Update()
``{ int i += Time.deltaTime * Speed; Door.rotation = Quaternion.Euler(new Vector3 (0, i, 0)); }
hope this answers your question!
I'll have a try, this looks promising, but I think I found my own solution!