- Home /
My camera stays in the same position regardless of any variables I change
I am following Burgzergarcade's tutorials and I think the camera position and angle are being set both in the GameMaster script and the HackandSlashCamera script. I am just trying to make the camera start in a different spot behind the player but no matter what I comment out or change the camera stays in the same spot.
here is the code for the GameMaster script:
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public GameObject playerCharacter;
public GameObject gameSettings;
public Camera mainCamera;
public float zOffset;
public float yOffset;
public float xRotOffset;
private GameObject _pc;
private PlayerCharacter _pcScript;
private Vector3 _playerSpawnPointPos; //this is the place in 3d space where I want my player to spawn
// Use this for initialization
void Start () {
_playerSpawnPointPos = new Vector3(35, 5, 120); //the default position for our player spawn point
GameObject go = GameObject.Find(GameSettings.PLAYER_SPAWN_POINT);
if(go == null){
Debug.Log("Can not find Player Spawn Point");
go = new GameObject(GameSettings.PLAYER_SPAWN_POINT);
Debug.Log("Created Player Spawn Point");
go.transform.position = _playerSpawnPointPos;
Debug.Log("Moved Player Spawn Point");
}
_pc = Instantiate(playerCharacter, go.transform.position, Quaternion.identity) as GameObject;
_pc.name = "pc";
_pcScript = _pc.GetComponent<PlayerCharacter>();
zOffset = 2.5f;
yOffset = 2.5f;
xRotOffset = 22.5f;
mainCamera.transform.position = new Vector3(_pc.transform.position.x, _pc.transform.position.y + yOffset, _pc.transform.position.z + zOffset);
mainCamera.transform.Rotate(xRotOffset, 0, 0);
LoadCharacter();
}
public void LoadCharacter() {
GameObject gs = GameObject.Find("__GameSettings");
if(gs == null) {
GameObject gs1 = Instantiate(gameSettings, Vector3.zero, Quaternion.identity) as GameObject;
gs1.name = "__GameSettings";
}
GameSettings gsScript = GameObject.Find ("__GameSettings").GetComponent<GameSettings>();
//loading the character data
gsScript.LoadCharacterData();
}
}
here is the code for the HackandSlashcamera script:
using UnityEngine;
using System.Collections;
public class HackAndSlashCamera : MonoBehaviour {
public Transform target;
public string playerTagName = "Player";
public float walkDistance = 4;
public float runDistance = 6;
public float height = 4;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
private Transform _myTransform;
private float _x;
private float _y;
private bool _camButtonDown = false;
void Awake(){
_myTransform = transform; //cache our transform so we do not need to look it up all of the time
}
// Use this for initialization
void Start () {
//if we do not have a target, let them know, else set the camera up according to where our target is.
if(target == null)
Debug.LogWarning ("We do not have a target for the camera");
else {
CameraSetUp();
}
}
void Update(){
//detetct if the player has entered any input
if(Input.GetMouseButtonDown(1)) { //Use the Input Manager to make this user selectable button
_camButtonDown = true;
}
if(Input.GetMouseButtonUp(1)) { //Use the Input Manager to make this user selectable button
_camButtonDown = false;
}
}
//this function is called after all of the Update functions are done
void LateUpdate(){
if(target !=null) {
if(_camButtonDown){
_x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
_y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
// y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else {
_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
_myTransform.LookAt(target);
_x = 0; //reset the x value
_y = 0; //reset the y value
// 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
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;
// Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z);
// Always look at the target
_myTransform.LookAt (target);
}
}
else {
GameObject go = GameObject.FindGameObjectWithTag(playerTagName);
if(go == null)
return;
target = go.transform;
}
}
public void CameraSetUp(){
_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
_myTransform.LookAt(target);
}
}
Answer by Chaos Warp · Mar 06, 2013 at 11:40 PM
Nevermind, the code for the camera is in both scripts so I just commented one out and started to fix the other.
Your answer
Follow this Question
Related Questions
Camera control of Tanks! tutorial..... 1 Answer
How to make camera position relative to a specific target. 1 Answer
Camera Falling when I press play. 0 Answers
Scripting question 3 Answers