I’m writing a four player local multiplayer game for a university assignment. The designers have a made a cyclic level so that you begin and finish in the same position and the characters in the game must run through the round level.
However, I have the issue that once the character has reached the first corner of the level and run around it, when the camera rotates behind the player, the controls remain mapped to the world coordinates. Therefore forward will make the character run right for example instead of forward. What is the best way to combat the issue(apart from level redesign!)? Here is a my camera script.
`
////////////////////////////////////////////////////////////////////
//IMPORTANT! Tag ALL players with “Player” so they are recognized.//
////////////////////////////////////////////////////////////////////
var targets : GameObject;
var currentDistance : float;
var largestDistance : float;
var theCamera : Camera;
var height : float ;
var avgDistance;
var distance = 0.0; // Default Distance
var speed = 1;
var offset : float;
//========================================
function Start()
{
targets = GameObject.FindGameObjectsWithTag("Player");
}
function LateUpdate ()
{
targets = GameObject.FindGameObjectsWithTag("Player");
if (!GameObject.FindWithTag("Player"))
{
return;
}
var sum = Vector3(0,0,0);
for (n = 0; n < targets.length ; n++)
{
sum += targets[n].transform.position;
}
avgDistance = sum / targets.length;
var largestDifference = returnLargestDifference();
height = Mathf.Lerp(height,largestDifference,Time.deltaTime * 2 * speed);
theCamera.transform.position.x = avgDistance.x;
theCamera.transform.position.z = avgDistance.z - 3;
theCamera.transform.position.y = avgDistance.y + 1;
theCamera.transform.LookAt(avgDistance);
if(theCamera.transform.position.z > 78)
{
theCamera.transform.rotation = Quaternion.Euler(0,-65,0);
theCamera.transform.position.x = theCamera.transform.position.x + 4;
theCamera.transform.position.y = theCamera.transform.position.y;
theCamera.transform.position.z = theCamera.transform.position.z + 2;
}
}
function returnLargestDifference()
{
currentDistance = 0.0;
largestDistance = 0.0;
for(var i = 0; i < targets.length; i++)
{
for(var j = 0; j < targets.length; j++)
{
currentDistance = Vector3.Distance(targets*.transform.position,targets[j].transform.position);*
-
if(currentDistance > largestDistance)*
{
-
largestDistance = currentDistance;*
-
}*
-
}*
-
}*
-
return largestDistance;*
-
}*
The camera zooms in and out depending on the distance that the players are apart.
If you need any more of the code I’m using I’ll happily post. Any advice/info would be great as this is really annoying me!
Cheers, Peter