How do I setup a camera that functions like the camera in Diablo or Torchlight, one that moves with the player, but does not rotate?
Also, the player needs to move like the characters in Diablo.
How do I setup a camera that functions like the camera in Diablo or Torchlight, one that moves with the player, but does not rotate?
Also, the player needs to move like the characters in Diablo.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject player; //The offset of the camera to centrate the player in the X axis
public float offsetX = -5; //The offset of the camera to centrate the player in the Z axis
public float offsetZ = 0; //The maximum distance permited to the camera to be far from the player, its used to make a smooth movement
public float maximumDistance = 2; //The velocity of your player, used to determine que speed of the camera
public float playerVelocity = 10;
private float movementX;
private float movementZ;
// Update is called once per frame
void Update ()
{
movementX = ((player.transform.position.x + offsetX - this.transform.position.x))/maximumDistance;
movementZ = ((player.transform.position.z + offsetZ - this.transform.position.z))/maximumDistance;
this.transform.position += new Vector3((movementX * playerVelocity * Time.deltaTime), 0, (movementZ * playerVelocity * Time.deltaTime));
}
}
Put this script in a camera in your game, adjust the variables to your needs, you have to drop your Player to the variable "player". Hope this helps!
This is the exactly the same code as above just nicely formatted.
Thank you @LucasGasper
using UnityEngine;
using System.Collections;
public class CameraControlScript : MonoBehaviour
{
public GameObject player;
//The offset of the camera to centrate the player in the X axis
public float offsetX = -5;
//The offset of the camera to centrate the player in the Z axis
public float offsetZ = 0;
//The maximum distance permited to the camera to be far from the player, its used to make a smooth movement
public float maximumDistance = 2;
//The velocity of your player, used to determine que speed of the camera
public float playerVelocity = 10;
private float movementX;
private float movementZ;
// Update is called once per frame
void Update()
{
movementX = ((player.transform.position.x + offsetX - this.transform.position.x))/maximumDistance;
movementZ = ((player.transform.position.z + offsetZ - this.transform.position.z))/maximumDistance;
this.transform.position += new Vector3((movementX * playerVelocity * Time.deltaTime),0, (movementZ * playerVelocity * Time.deltaTime));
}
}
If you're not interested in rotation or anything fancy, you can just "lock" the camera on your player model by making the camera a child of the character - just drag the camera in the hierarchy on top of your character in the hierarchy. Then, in the editor, move your view (the actual editor view, not the camera object) so it looks at the character exactly the way you want the camera to look at your character. Then select the camera in the hierarchy and choose "align with view" from the "GameObject" menu at the top. That will position the camera at that view. When you run the game, the camera should move along with your player.
I suggest to add Y axis, If you wont look like izometric set camera rotation manualy x=30, y=40, z=0.
Y axis will correct a height of camera.
public class IzoCamera : MonoBehaviour
{
public GameObject Player;
public float OffsetX = -35;
public float OffsetZ = -40;
public float OffsetY = 30;
public float MaximumDistance = 2;
public float PlayerVelocity = 10;
private float _movmentX;
private float _movmentY;
private float _movmentZ;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
_movmentX = ((Player.transform.position.x + OffsetX - this.transform.position.x)) / MaximumDistance;
_movmentY = ((Player.transform.position.y + OffsetY - this.transform.position.y)) / MaximumDistance;
_movmentZ = ((Player.transform.position.z + OffsetZ - this.transform.position.z)) / MaximumDistance;
this.transform.position += new Vector3((_movmentX * PlayerVelocity * Time.deltaTime), (_movmentY * PlayerVelocity * Time.deltaTime), (_movmentZ * PlayerVelocity * Time.deltaTime));
}
}