[Solved]Camera question

Hi all … my question is, how can move the camera up and down when i walk?. I’m using FPS prefab for Player.

Sorry for my english :slight_smile:

An easy way would be to implement looping coroutines.

In psuedo-code:

if (the player is moving)
{
  if (not bobbing up and down)
  {
    BobUpAndDown();
  }
}

function BobUpAndDown()
{
  player is bobbing up and down = true;  

  for (i=0.0, i<1.0, i+=Time.deltaTime)
  {
    if (player is not moving)     // We don't want to continue the bob if the player isn't moving
    {
      player is bobbing up and down = false;
      return;
    }
    
    if (i<.5)  // Halfway there
      Camera.main.transform.Translate(Vector3.up * Time.deltaTime);  // Move the camera up
    else
      Camera.main.transform.Translate(-Vector3.up * Time.deltaTime);  // Move the camera down

    yield;  // You must have this or all the stuff above will happen at once, instead of over time
  }

  player is bobbing up and down = false;  
}

Of course this code is terribly slow and unoptimized, but it looked like you wanted something simple.

I’m noob with script, i try use this code:

var walk = true;

function Update(){

	while (walk){
		Camera.main.transform.Translate(Vector3.up * Time.deltaTime);
		yield WaitForSeconds(1.0);
		Camera.main.transform.Translate(-Vector3.up * Time.deltaTime);
		yield WaitForSeconds(1.0);
	}

}

while “walk” is “true” execute the code but i get this error: Update() can not be a coroutine.

Put the if (the player is moving) part in your update loop, and the other function outside the update loop.

Well now works fine, but is soooo ugly “effect” hahaha :cry:

Smooth it out by using a function, such as an inverted parabola (-x^2), to dictate the height.

Well i found another solution… “Animation View”. Creating an animation camera and play it when i press “w” key :slight_smile:

Thx for your help cjcurrie and again sorry for my bad english :roll:

hi,
try this,

var ypos : float;
var speed : float = 5;
var walking : boolean;
function Start(){
  ypos = transform.localPosition.y;
}
function Update(){
  if(walking){
    transform.localPosition.y = Mathf.Lerp(transform.localPosition.y,Mathf.Sin(Time.time*speed)+ypos,Time.deltaTime*5);
  }else{
    transform.localPosition.y = Mathf.Lerp(transform.localPosition.y,ypos,Time.deltaTime*5);
  }
}

I hope it help you. :smile:

Hey thx dude!! is working perfect! :slight_smile: thx a lot :smile:

Off topic:
A fan of Axel Rose eh? :stuck_out_tongue:

hahaah guns and roses FTW (but the old GNR) hahaha 8)