- Home /
The question is answered, right answer was accepted
Diablo Style Camera and Movement
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.
Answer by LucasGaspar · Sep 21, 2010 at 04:16 AM
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!
I got the following error when I pasted the code in a in a blank C# script: The importer for asset Assets/$$anonymous$$odels/Scripts/NewBehaviourScript.cs ($$anonymous$$onoImporter) did not generate a $$anonymous$$D4 digest.
I know this is old, but I have a question about this. When thrown onto my third person controller, when I hit play, the camera focuses somewhere on the ground where I can't see the player. I dragged my "3rd person controller" to the player variable. Is this correct usage?
Answer by fscopel · Sep 17, 2016 at 12:51 AM
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));
}
}
Answer by Manfice · Nov 03, 2016 at 12:53 PM
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));
}
}
Answer by Graham · Sep 21, 2010 at 03:36 AM
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 would actually only like the camera to translate with the character, but not rotate. So the cameras rotation is locked to the world.
is there a JavaScript equivalent for this script? I tried translating it but I'm a noob :P