Hey guys,
I’m currently trying to set the camera’s position by the average position of a number of object positions. The average value seems to be correct, however setting the camera’s position doesn’t seem to change anything. The weird thing is, if I run the game, modify the script ( add a space or new line for example ) and save the script whilst the game is running, the camera’s position changes properly.
I was wondering if there was anything I may have overlooked during the game’s load that might not allow me to change the camera’s position?
Code (problem occurs in the update function):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
// managers the characters.
public class CharacterManager : MonoBehaviour {
// member variables.
private List<Character> m_aCharacterFact;
private bool m_bMouseDown = false;
private Vector3 m_vDirection;
private float m_fCameraZoom = 0.0f;
private Vector3 m_vAveragePosition;
// member methods.
private void ParseXML( string a_strFilename )
{
// make sure we create our array.
m_aCharacterFact = new List<Character>();
// parse xml.
XmlDocument xmlDoc= new XmlDocument(); // create an xml document object.
xmlDoc.Load( a_strFilename );
XmlNodeList characters = xmlDoc.GetElementsByTagName("character");
for (int i = 0; i < characters.Count; ++i)
{
GameObject tempobject = Instantiate( Resources.Load("Character") ) as GameObject;
Character temp = tempobject.GetComponent<Character>();
float scale = Random.Range(1.0f, 0.7f);
temp.init( float.Parse( characters[i].Attributes["x"].Value ), float.Parse( characters[i].Attributes["y"].Value ), scale );
// add pattern.
m_aCharacterFact.Add( temp );
}
}
// Use this for initialization
void Start ()
{
m_bMouseDown = false;
m_vDirection = Vector3.zero;
m_vAveragePosition = Vector3.zero;
Vector3 cameraTransform = Camera.mainCamera.transform.position;
m_fCameraZoom = cameraTransform.z;
ParseXML( "Assets/data/spawndata.xml" );
}
// Update is called once per frame
void Update ()
{
if ( Input.GetKeyDown(KeyCode.A) )
m_vDirection.x += 1.0f;
if ( Input.GetKeyUp(KeyCode.A) )
m_vDirection.x -= 1.0f;
if ( Input.GetKeyDown(KeyCode.D) )
m_vDirection.x -= 1.0f;
if ( Input.GetKeyUp(KeyCode.D) )
m_vDirection.x += 1.0f;
// handles character movement.
m_vAveragePosition = Vector3.zero;
foreach ( Character temp in m_aCharacterFact )
{
temp.transform.position += m_vDirection * Time.deltaTime;
m_vAveragePosition += temp.transform.position;
}
m_vAveragePosition /= m_aCharacterFact.Capacity;
m_vAveragePosition.z = m_fCameraZoom;
Camera.mainCamera.transform.position = m_vAveragePosition;
}
void LateUpdate()
{
print (Camera.mainCamera.transform.position);
}
}
Thanks,