- Home /
Make my camera turn smoothly
My script does that when i collide with a special gameobject, the camera points at the enemy. It all works but the camera just looks directly at the enemy, and does not turn slowly. How do i make it turn smoothly ?
Answer by azmat786n · Dec 26, 2013 at 04:08 PM
var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;
function Update ()
{
var wantedPosition = target.TransformPoint(0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
Answer by NinjaRubberBand · Dec 26, 2013 at 05:27 PM
Oh it didn't take my script with, here is the script that i made:
pragma strict
var target : Transform; var look : boolean = false;
function OnTriggerEnter (other : Collider) { if(other.tag == "Wall")
GameObject.Find( "First Person Controller" ).GetComponent(FPSInputController).enabled = false;
GameObject.Find( "First Person Controller" ).GetComponent(CharacterMotor).enabled = false;
GameObject.Find( "First Person Controller" ).GetComponent(MouseLook).enabled = false;
GameObject.Find( "Main Camera" ).GetComponent(MouseLook).enabled = false;
look = true;
yield(WaitForSeconds(5));
GameObject.Find( "First Person Controller" ).GetComponent(FPSInputController).enabled = true;
GameObject.Find( "First Person Controller" ).GetComponent(CharacterMotor).enabled = true;
GameObject.Find( "First Person Controller" ).GetComponent(MouseLook).enabled = true;
GameObject.Find( "Main Camera" ).GetComponent(MouseLook).enabled = true;
look = false;
}
function Update () {
if(look == true)
transform.LookAt(target);
}
var target : Transform;
var look : boolean = false;
var fpsInput : FPSInputController;
var character$$anonymous$$otor : Character$$anonymous$$otor;
var mouseLook : $$anonymous$$ouseLook;
var mouseLook2 : $$anonymous$$ouseLook;
function Start() {
fpsInput = GameObject.Find("First Person Controller").GetComponent(FPSInputController);
character$$anonymous$$otor = GameObject.Find("First Person Controller").GetComponent(Character$$anonymous$$otor);
mouseLook = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLook);
mouseLook2 = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLook);
}
function OnTriggerEnter (other : Collider) {
if(other.tag == "Wall") {
DisableComponents();
}
}
function DisableComponents() {
fpsInput.enabled = false;
character$$anonymous$$otor.enabled = false;
mouseLook.enabled = false;
mouseLook2.enabled = false;
look = true;
yield WaitForSeconds(5);
EnableComponents();
look = false;
}
function EnableComponents() {
fpsInput.enabled = true;
character$$anonymous$$otor.enabled = true;
mouseLook.enabled = true;
mouseLook2.enabled = true;
}
function Update () {
if(look == true) {
transform.LookAt(target);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613124902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate camera with easeout 0 Answers
Smooth Camera Rotate 2 Answers
How to rotate the camera smoothly for just 1 second? 2 Answers
camera smooth stop (continuously sliding), on touch end 0 Answers
Turn page, smooth rotate 1 Answer