Hello everyone!
I’m just trying to zoom in and out, depending on whether there is something blocking the view or not, but each time anything blocks the view, the Editor crashes.
I don’t know, am I doing something wrong here?
var CharacterCameraDistance0 : float;
var CharacterCameraDistance_least : float = 1.5;
function start__set_camera_distance0 () {
CharacterCameraDistance0 = (
camera_obj.transform.position-Character_gameobject.transform.position).magnitude;
}
functionCameraAdjustment () {
var CharacterCameraDirection : Vector3 =
(camera_obj.transform.position-Character_gameobject.transform.position);
var CharacterCameraDistance : float = CharacterCameraDirection.magnitude;
var RayCastHit : RaycastHit;
if (Physics.Raycast(
Character_gameobject.transform.position,CharacterCameraDirection,
RayCastHit,CharacterCameraDistance)
) {
//CAMERA NEARER
if (RayCastHit.transform.gameObject.tag != "Character") {
while (CharacterCameraDistance > CharacterCameraDistance_least) {
move_camera__towards();
}
}
//CAMERAAWAY
else {
while (CharacterCameraDistance < CharacterCameraDistance0) {
move_camera__away();
}
}
}
}
Oh, and move_camera_towards() and move_camera__away() are working on their own.
You have an infinite while loop. During the loop, the game isn’t updating, so the camera doesn’t move, so the condition that something is blocking the camera is always true. Because the loop is infinite, the application will cease responding.
You can’t use while loops like that in Update(). In fact, whenever you write “while” in the Update() loop, a mental alarm bell should be ringing so you don’t get into this kind of situation. Never forget that Update() gets called every frame, and all code inside Update() needs to execute in its entirety before the frame is rendererd.
You can fix it by getting rid of the while loop, and just moving the camera a bit if it is being blocked. If the camera is being blocked in subsequent frames, the camera will move as you want it to. Using Vector3.MoveTowards() makes this very easy.
Alright, this makes sense. Thanks for the information!
So I could use a boolean to lock the loop, couldn’t I? So it is only called once? And when it ends, I set the boolean back?
I will probably use Vector3.MoveTowars() as you suggested. My questions are just out of curiosity.
Just as an example:
var CharacterCameraDistance0 : float;
var CharacterCameraDistance_least : float = 1.5;
function start__set_camera_distance0 () {
CharacterCameraDistance0 = (
camera_obj.transform.position-Character_gameobject.transform.position).magnitude;
}
functionCameraAdjustment () {
var CharacterCameraDirection : Vector3 =
(camera_obj.transform.position-Character_gameobject.transform.position);
var CharacterCameraDistance : float = CharacterCameraDirection.magnitude;
var RayCastHit : RaycastHit;
if (Physics.Raycast(
Character_gameobject.transform.position,CharacterCameraDirection,
RayCastHit,CharacterCameraDistance)
) {
//CAMERA NEARER
var IsMovingTowards : boolean = false;
if (!IsMovingTowards) {IsMovingTowards = true;
if (RayCastHit.transform.gameObject.tag != "Character") {
while (CharacterCameraDistance > CharacterCameraDistance_least) {
move_camera__towards();
}
}
IsMovingTowards = false;
}
//CAMERAAWAY
var IsMovingAway : boolean = false;
if (!IsMovingAway) {IsMovingAway = true;
else {
while (CharacterCameraDistance < CharacterCameraDistance0) {
move_camera__away();
}
}
IsMovingAway = false;
}
}
}