- Home /
Need help with camera for 2 player roll a ball.
Hi, I'm new to unity scripting, and I made a 2 player basketball game from the unity roll a ball tutorial. The camera works great with one ball(when the ball touches the fence behind the basketball hoop, you can still see the ball and hoop). But when I add the second ball, the camera works fine until both balls move at the same time, and the camera moves twice the distance and clips through the fence and the wall, and all I can see is the skybox. It seems like the camera doubles it's movement of the 2 balls, even the jump camera height is doubled. The only workaround I could do is move the camera farther away(once I did that, the other side of the court would show the skybox), but I would like to keep the camera at it's original position. Here's the code below, if someone could help fix it or at least show me the right direction to fix the code, it would be much appreciated.
using UnityEngine;
using System.Collections;
public class CameraControllerV1 : MonoBehaviour {
//Attach this script to your camera.
public GameObject Player; // Add player 1
public GameObject Player2; // Add player 2
//These Variables are used to offset the camera away from the object. The values can be edited in the Object Editor.
public float distanceFromCamZ; // Use a negative number to increase the distance to the object on the Z Axis.
public float distanceFromCamY; // Use a positive number to increase the height of the camera above the object.
void LateUpdate () {
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x,
Player.gameObject.transform.position.y + Player2.gameObject.transform.position.y + distanceFromCamY,
Player.gameObject.transform.position.z + Player2.gameObject.transform.position.z + distanceFromCamZ) ;
// Start the game and adjust the values in the editor while it's running to get your ideal settings.
}
}
Thanks.
Answer by Milmac11 · Dec 13, 2015 at 11:54 PM
I fixed the code by adding an empty gameobject and attached this code to it:
using UnityEngine;
using System.Collections;
public class FindMidPoint : MonoBehaviour {
public GameObject player1;
public GameObject player2;
private Vector3 midpoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
midpoint = (player1.gameObject.transform.position + player2.gameObject.transform.position) * .5f;
transform.position = midpoint;
}
}
and replaced the camera script with this code:
using UnityEngine;
using System.Collections;
public class CameraControlV4 : MonoBehaviour {
public GameObject cameraFocusPoint;
public Transform cameraFocus;
public float distanceFromCamZ; // Use a negative number to increase the distance to the object on the Z Axis.
public float distanceFromCamY; // Use a positive number to increase the height of the camera above the object.
// Update is called once per frame
void Update () {
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x,
cameraFocusPoint.gameObject.transform.position.y + distanceFromCamY,
cameraFocusPoint.gameObject.transform.position.z + distanceFromCamZ);
}
}
And it works perfectly :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612052242im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
need help with roll a ball like game 0 Answers
How to make camera position relative to a specific target. 1 Answer
transform.lookat() limitation... 1 Answer
Second play may not be able to leave view 0 Answers
Roll-a-ball tutorial does not work. 1 Answer