When I jump with my player the camera moves up with the player. Camera is a parent of player to get that following horizontal look.
But, i need to disable the the camera’s y position when i jump to make it look smoother. Here’s a webplayer. Spacebar to jump.
https://dl.dropbox.com/u/56545886/ParanormalRunner/ParanormalRunner.html
using UnityEngine;
using System.Collections;
public class scrHudson : MonoBehaviour {
//Variables
private scrAnimationControls animScript;
public Transform death;
const float constantMove = 1;
//If it really is a constant speed you can declare it as such and it will prevent you from changing it.
//Otherwise just remove the modifier.
public float speed = 0.001f;
public float jumpForce = 1f;
//how fast do we move side to side and jump.
public float gravity = 9.8f;
private CharacterController controller;
public Transform mainCam;
public Vector3 fixedCamera;
public bool stopCamera = false;
// Use this for initialization
void Start ()
{
animScript = GameObject.Find("GameController").GetComponent<scrAnimationControls>();
controller = GetComponent<CharacterController>();
mainCam = GetComponent<Transform>();
jumpForce = 102f;
//cache a link to the Character Controller.
}
// Update is called once per frame
void Update ()
{
FallDeath();
Vector3 moveDirection = new Vector3( 0 , 0 , constantMove );
//Create a new Direction vector
if( controller.isGrounded)
{ //Are we touching the ground?
moveDirection.x = Input.GetAxis("Horizontal") * speed;
animation.CrossFade("run");
if( Input.GetKeyDown("space") )
{
stopCamera = true;
animation.CrossFade("jump");
if(animation.IsPlaying("jump"))
moveDirection.y = jumpForce;
//Set the vertical direction to the jump speed.
moveDirection = transform.TransformDirection(moveDirection);
//You don't need this line since you will only be moving in one direction,
//But you will if your character rotates any.
}
}
moveDirection.y -= gravity * Time.deltaTime;
//This will pull the character back down.
controller.Move(moveDirection * Time.deltaTime);
//Move the character in the direction of moveDirection at a FPS independent rate.
if(!controller. isGrounded)
{
animation.CrossFade("fall");
}
if(stopCamera)
{
//mainCam.transform.position = fixedCamera;
// stopCamera = false;
}
}
void FallDeath()
{
if(controller.transform.position.y <= -0.4f)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}