I wanted to make a rpg, and I had everything going great, until I needed a camera that can follow the character in a way that it was playable to the player. what I need is camera that moves up in the y positions the higher up the character goes, and down in y positions when the character goes lower.If the character doesn’t move, then the camera doesn’t move. also when the character moves the camera follow and move along with it,and the character must be on screen at all times.Camera must also not move in Rotation X, Y, or Z
There’s a full third person tutorial available from Unity. http://unity3d.com/support/resources/example-projects/3rdpersonshooter
You can find an orbiting camera in there - or there’s one here.
The simplest approach is just to put the camera as a child of the character by the way.
This script may help you to follow the player. Attach this script to main camera and give it a target to look at.
using UnityEngine;
using System.Collections;
// Place the script in the Camera-Control group in the component menu
[AddComponentMenu("Camera-Control/Smooth Follow")]
public class smoothFollow : MonoBehaviour
{
/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 5.0f;
public static smoothFollow Instance;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
void Start()
{
Instance=this;
}
void LateUpdate ()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera using temp variable
Vector3 temp=transform.position;
temp.y=currentHeight;
transform.position = temp;
// Always look at the target
transform.LookAt (target);
}
}