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 masonb21 · Oct 09, 2016 at 07:48 AM · c#unity 5camera

how to switch cameras C#

I made this code here so the cameras switch each turn, but it doesn't work. Anyone know how to fix?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BoardManager : MonoBehaviour
 {
     public static BoardManager Instance { set; get; }
     private bool[,] allowedMoves { set; get; }
 
     public Chessman[,] Chessmans { set; get; }
     private Chessman selectedChessman;
 
     private const float TILE_SIZE = 1.0f;
     private const float TILE_OFFSET = 0.5f;
 
     private int selectionX = -1;
     private int selectionY = -1;
 
     public List<GameObject> chessmanPrefabs;
     private List<GameObject> activeChessman;
 
     private Material previousMat;
     public Material selectedMat;
 
     public int[] EnPassantMove { set; get; }
 
     private Quaternion orientation = Quaternion.Euler(0, 180, 0);
 
     public bool isWhiteTurn = true;
 
     public GameObject camera1;
     public GameObject camera2;
 
     private void Start()
     {
         Instance = this;
         SpawnAllChessman ();
 
         camera1.SetActive(true);
         camera2.SetActive(false);
     }
 
     private void Update()
     {
         UpdateSelection();
         DrawChessBoard();;
 
         if (Input.GetMouseButtonDown(0))
         {
             if (selectionX >= 0 && selectionY >= 0)
             {
                 if (selectedChessman == null)
                 {
                     //Select the chessman
                     SelectChessman(selectionX, selectionY);
                 }
                 else
                 {
                     //Move the chessman
                     MoveChessman(selectionX, selectionY);
                 }
             }
         }
 
         if (isWhiteTurn == false)
         {
             camera1.SetActive(false);
             camera2.SetActive(true);
         }
         else
         {
             camera1.SetActive(true);
             camera2.SetActive(false);
         }
     }
 
     private void SelectChessman(int x, int y)
     {
         if (Chessmans[x, y] == null)
             return;
 
         if (Chessmans[x, y].isWhite != isWhiteTurn)
             return;
 
         bool hasAtLeastOneMove = false;
         allowedMoves = Chessmans[x, y].PossibleMove();
         for (int i = 0; i < 8; i++)
             for (int j = 0; j < 8; j++)
                 if (allowedMoves[i, j])
                     hasAtLeastOneMove = true;
 
         selectedChessman = Chessmans[x, y];
         previousMat = selectedChessman.GetComponent<MeshRenderer>().material;
         selectedMat.mainTexture = previousMat.mainTexture;
         selectedChessman.GetComponent<MeshRenderer>().material = selectedMat;
         BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
     }
 
     private void MoveChessman(int x, int y)
     {
         if (allowedMoves[x,y])
         {
             Chessman c = Chessmans[x, y];
 
             if(c != null && c.isWhite != isWhiteTurn)
             {
                 //Capture a piece
 
                 //If it is the king
                 if (c.GetType () == typeof(King))
                 {
                     EndGame();
                     return;
                 }
 
                 activeChessman.Remove(c.gameObject);
                 Destroy(c.gameObject);
             }
 
             if(x == EnPassantMove[0] && y == EnPassantMove[1])
             {
                 if(isWhiteTurn)
                     c = Chessmans[x, y - 1];
                 else
                     c = Chessmans[x, y + 1];
 
                 activeChessman.Remove(c.gameObject);
                 Destroy(c.gameObject);
 
             }
             EnPassantMove[0] = -1;
             EnPassantMove[1] = -1;
             if(selectedChessman.GetType () == typeof(Pawn))
             {
                 if (y == 7)
                 {
                     activeChessman.Remove(selectedChessman.gameObject);
                     Destroy(selectedChessman.gameObject);
                     SpawnWhiteChessman(1, x, y);
                     selectedChessman = Chessmans[x, y];
                 }
                 else if (y == 0)
                 {
                     activeChessman.Remove(selectedChessman.gameObject);
                     Destroy(selectedChessman.gameObject);
                     SpawnWhiteChessman(7, x, y);
                 }
 
                 if (selectedChessman.CurrentY == 1 && y == 3)
                 {
                     EnPassantMove[0] = x;
                     EnPassantMove[1] = y - 1;
                 }
                 else if (selectedChessman.CurrentY == 6 && y == 4)
                 {
                     EnPassantMove[0] = x;
                     EnPassantMove[1] = y + 1;
                 }
             }
 
             Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
             selectedChessman.transform.position = GetTileCenter(x, y);
             selectedChessman.SetPosition(x, y);
             Chessmans [x, y] = selectedChessman;
             isWhiteTurn = !isWhiteTurn;
         }
 
         selectedChessman.GetComponent<MeshRenderer>().material = previousMat;
         BoardHighlights.Instance.Hidehighlights();
         selectedChessman = null;
     }
 
     private void UpdateSelection()
     {
         if (!Camera.main)
             return;
 
         RaycastHit hit;
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit, 25.0f, LayerMask.GetMask("ChessPlane")))
         {
             selectionX = (int)hit.point.x;
             selectionY = (int)hit.point.z;
         }
         else
         {
             selectionX = -1;
             selectionY = -1;
         }
     }
 
     private void SpawnWhiteChessman(int index, int x, int y)
     {
         GameObject go = Instantiate(chessmanPrefabs[index], GetTileCenter(x,y), Quaternion.identity) as GameObject;
         go.transform.SetParent(transform);
         Chessmans[x, y] = go.GetComponent<Chessman>();
         Chessmans[x, y].SetPosition(x, y);
         activeChessman.Add(go);
     }
 
     private void SpawnBlackChessman(int index, int x, int y)
     {
         GameObject go = Instantiate(chessmanPrefabs[index], GetTileCenter(x,y), orientation) as GameObject;
         go.transform.SetParent(transform);
         Chessmans[x, y] = go.GetComponent<Chessman>();
         Chessmans[x, y].SetPosition(x, y);
         activeChessman.Add(go);
     }
 
     private void SpawnAllChessman()
     {
         activeChessman = new List<GameObject> ();
         Chessmans = new Chessman[8, 8];
         EnPassantMove = new int[2] { -1, -1 };
 
         //Spawn White Team
 
         //King
         SpawnWhiteChessman(0, 3, 0);
 
         //Queen
         SpawnWhiteChessman(1, 4, 0);
 
         //Rooks
         SpawnWhiteChessman(2, 0, 0);
         SpawnWhiteChessman(2, 7, 0);
 
         //Bishops
         SpawnWhiteChessman(3, 2, 0);
         SpawnWhiteChessman(3, 5, 0);
 
         //Knights
         SpawnWhiteChessman(4, 1, 0);
         SpawnWhiteChessman(4, 6, 0);
 
         //Pawns
         for (int i = 0; i < 8; i++)
             SpawnWhiteChessman(5, i, 1);
 
         //Spawn Black Team
 
         //King
         SpawnBlackChessman(6, 4, 7);
 
         //Queen
         SpawnBlackChessman(7, 3, 7);
 
         //Rooks
         SpawnBlackChessman(8, 0, 7);
         SpawnBlackChessman(8, 7, 7);
 
         //Bishops
         SpawnBlackChessman(9, 2,7);
         SpawnBlackChessman(9, 5, 7);
 
         //Knights
         SpawnBlackChessman(10, 1, 7);
         SpawnBlackChessman(10, 6, 7);
 
         //Pawns
         for (int i = 0; i < 8; i++)
             SpawnBlackChessman(11, i, 6);
     }
 
     private Vector3 GetTileCenter(int x, int y)
     {
         Vector3 origin = Vector3.zero;
         origin.x += (TILE_SIZE * x) + TILE_OFFSET;
         origin.z += (TILE_SIZE * y) + TILE_OFFSET;
         return origin;
     }
 
     private void DrawChessBoard()
     {
         Vector3 widthLine = Vector3.right * 8;
         Vector3 heightLine = Vector3.forward * 8;
 
         for (int i = 0; i <= 8; i++)
         {
             Vector3 start = Vector3.forward * i;
             Debug.DrawLine(start, start + widthLine);
             for (int j = 0; j <= 8; j++)
             {
                 start = Vector3.right * j;
                 Debug.DrawLine(start, start + heightLine);
             }
         }
 
         // Draw the selection
         if (selectionX >= 0 && selectionY >= 0)
         {
             Debug.DrawLine(
                 Vector3.forward * selectionY + Vector3.right * selectionX,
                 Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
 
             Debug.DrawLine(
                Vector3.forward * (selectionY + 1) + Vector3.right * selectionX,
                Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
         }
     }
 
     private void EndGame()
     {
         if (isWhiteTurn)
             Debug.Log("White team wins");
         else
             Debug.Log("Black team wins");
 
         foreach (GameObject go in activeChessman)
             Destroy(go);
 
         isWhiteTurn = true;
         BoardHighlights.Instance.Hidehighlights();
         SpawnAllChessman();
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't do Quaternion.AngleAxis twice. 1 Answer

Pinch Zoom and Pan while not calling other functions 1 Answer

How to access camera preview frames with MediaCapture Class in Unity? 0 Answers

I cannot see button on camera preview 1 Answer

Scope Zoom Sensitivity 0 Answers


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