Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Milmac11 · Dec 13, 2015 at 02:57 PM · cameraroll a ball2 player

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. 
 
     }
 }

alt text Thanks.

1-and-2-players-compressed.jpg (412.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges