- Home /
Turning the camera down smoothly while running
How can I turn the camera down smoothly for xx degrees while running and returns to his original state while walking?
I only need the script for moving the camera down and up, can someone give me an example?
Answer by gribbly · Mar 24, 2013 at 09:23 PM
A very easy way to do this:
Create an empty game object called "LookTarget" as a child of your character
In your camera script, have the camera look at LookTarget every frame:
Transform lookTarget = GameObject.Find("LookTarget").transform; //put this in Start()
transform.LookAt(lookTarget); //put this in Update()
In your character script, move LoookTarget according to character state... you can do simple vertical moves on the Y axis and the camera will automatically track.
This method has the advantage of letting you get the behavior you want without dealing with rotations directly, which can be complex.
As an optimization, you may want to assign lookTarget in the inspector instead of using GameObject.Find.
Can you give me an example of step 3? I don't know how to do this in code..
Sure, so assu$$anonymous$$g you have a "LookTarget" object that is a child of your character object, you can do this in your character script.
First set up a variable so your character script can talk to LookTarget:
public var lookTarget:Transform;
...that wlll show up in the inspector when you select your character. Drag the LookTarget object onto it to assign it.
Now in your update function you can talk to the LookTarget object via the var lookTarget.
function Update(){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1)){
lookTarget.Translate(0.0f, 1.0f, 0.0f);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha2)){
lookTarget.Translate(0.0.f, -1.0f, 0.0f);
}
}
Now when you run the game you can press 1 to move LookTarget up, 2 to move it down.
When you have it working this way, you'll want to replace the Input.Get$$anonymous$$eyDown events with something relevant to your game - you wanted to link it to character state, you'll have to work out the details, but as long as you can do something like "if this state then..." it should work fine.
Answer by Josh707 · Mar 24, 2013 at 11:23 PM
You can do it with animations which work well as they interpolate if the timescale is lowered. Add an empty gameobject between the camera and the player and animate it rotating down slightly and then back to 0,0,0 rotation. You'll be able to control it normally but it will also be pulled down. This is what I did for left and right footsteps, it looks nice and this sample code should give you an idea.
if(moving == true){
if(left == true){
if(!anim.isPlaying){
anim.Play("walkLeft");
right = true;
left = false;
}
}
if(right == true){
if(!anim.isPlaying){
anim.Play("walkRight");
left = true;
right = false;
}
}
}
If you just want it going up and down, in Update() just do something like this:
if(moving == true){
if(!anim.IsPlaying){
anim.Play("lowerCamera");
}
}
When I use this approach my camera is freezed at certain rotations, I can only rotate up and down and sometimes with a wierd rotation to other points.
I've added the script to the empty GameObject, which is a child of the Player and the parent of the main camera.
Your answer
Follow this Question
Related Questions
Smooth Attached Object movement 1 Answer
How Can I Smooth This Camera Code 0 Answers
Camera, Look rotation, smoothly? 0 Answers
smoothly rotate camera around an object with momentum 0 Answers
Smooth Rotation 2 Answers