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 /
  • Help Room /
This question was closed Apr 18, 2018 at 06:16 PM by degeta for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by degeta · Apr 06, 2018 at 12:48 PM · unityeditor

Issue with the script!

alt text

When we click on one of the cubes it will shoot projectiles. I have created a function to decide which capsule will shoot i.e TurnDecider() So at the beginning Player1 is active and so only it can shoot a projectile. As soon as he shoots the function TurnDecider() is called to change turn to Player2. So logically only Player2 should be shooting but in the game both Player2 and Player3 are shooting.

  • Left: Player 2

  • Center: Player 1

  • Right: Player 3

The scripts are given below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameManage : MonoBehaviour
 {
 
     private int playernumber;
     private PlayerController a, b, c;
     public bool fired = false;
 
     public void InitialAssign()
     {
         playernumber = 1;
         a = GameObject.Find("Player (1)").GetComponent<PlayerController>();       
         b = GameObject.Find("Player (2)").GetComponent<PlayerController>();
         c = GameObject.Find("Player (3)").GetComponent<PlayerController>();
         TurnDecider();
     }
 
     void Start()
     {
         InitialAssign();
     }
 
     public void PlayerSelector()
     {
         if (playernumber == 3)
         {
             playernumber = 1;
         }
         else
         {
             playernumber++;
         }
         TurnDecider();
     }
 
     public void TurnDecider()
     {
         switch (playernumber)
         {
             case 1:
                 a.isReady=true;                      
                 b.isReady = false;
                 c.isReady = false;
                 Debug.Log("Player 1 is ready!");
                 break;
 
             case 2:
                 a.isReady = false;
                 b.isReady = true;
                 c.isReady = false;
                 Debug.Log("Player 2 is ready!");
                 break;
 
             case 3:
                 a.isReady = false;
                 b.isReady = false;
                 c.isReady = true;
                 Debug.Log("Player 3 is ready!");
                 break;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         if (fired == true)
         {
             PlayerSelector();
             Debug.Log(playernumber);
             fired = false;
         }
     }
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public GameObject projectile;
     public float Power = 10f;
     public float JumpPower;
     private Rigidbody rb;
     private Transform shooter;
 
     public GameManage gamescript;
     private string obj;
     public bool isReady = false;
     private bool isGround = false;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         shooter=transform.Find("Shooter");       
     }
 
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.CompareTag("Ground"))
         {
             isGround = true;
         }
     }
 
     public void Fire()
     {        
         GameObject ball = Instantiate(projectile, position: shooter.position, rotation: shooter.rotation);
         ball.GetComponent<Rigidbody>().velocity = (GameObject.Find(obj).transform.position - shooter.position).normalized * Power;
     }
 
     public void Jump()
     {
         if (isGround)
         {
             rb.velocity = new Vector3(0f, JumpPower, 0f);
         }
         isGround = false;
     }
     void Update()
     {
         if (Input.GetMouseButtonDown(0) && isReady && isGround)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.transform.tag == "Target")
                 {
                     Debug.Log("Fire Called by " + transform.name);
                     obj = hit.transform.name.ToString();
                     Fire();
                     gamescript.GetComponent<GameManage>().fired = true;
                 }
             }
         }
     }
 }

web.png (264.4 kB)
Comment
Add comment · Show 4
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
avatar image TreyH · Apr 06, 2018 at 02:44 PM 0
Share

Is there a reason you aren't giving the command from your $$anonymous$$anager? Having the manager sort out who's active is fine, but having the individual objects checking if they're allowed to fire from their own Updates feels a bit clunky. You can just do that from the $$anonymous$$anager's Update.

avatar image degeta TreyH · Apr 06, 2018 at 02:54 PM 0
Share

Like...? Can u explain in code?

avatar image TreyH degeta · Apr 06, 2018 at 03:14 PM 0
Share

This isn't a forum for code corrections, but I will help get you started. First, you can move most of that firing routine somewhere else.

 // Place this into your PlayerController.
 //
 public bool AttemptFire() {
 
     if (Input.Get$$anonymous$$ouseButtonDown(0) && isReady && isGround)
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit = new RaycastHit();
         if (Physics.Raycast(ray, out hit))
         {
             if (hit.transform.tag == "Target")
             {
                 Debug.Log("Fire Called by " + transform.name);
                 obj = hit.transform.name.ToString();
                 Fire();
 
                 // If we did fire
                 return true;
             }
         }
     }
 
     // If we didn't
     return false;
 }


Then have your manager keep track of which player is currently active

 private PlayerController activePlayer;


Then use your big switch body there to set activePlayer to whichever PlayerController instance should fire next. You can figure out the rest.

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by degeta · Apr 06, 2018 at 03:54 PM

Can u check if the code is proper? And can this code be improved?

 public class PlayerController : MonoBehaviour
 {
     public GameObject projectile;
     public float Power = 10f;
     public float JumpPower;
     private Rigidbody rb;
     private Transform shooter;
 
     public GameManage gamescript;
     private string obj;
     public bool isReady = false;
     private bool isGround = false;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         shooter=transform.Find("Shooter");       
     }
 
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.CompareTag("Ground"))
         {
             isGround = true;
         }
     }
 
     public void Fire()
     {        
         GameObject ball = Instantiate(projectile, position: shooter.position, rotation: shooter.rotation);
         ball.GetComponent<Rigidbody>().velocity = (GameObject.Find(obj).transform.position - shooter.position).normalized * Power;
     }
 
     public void Jump()
     {
         if (isGround)
         {
             rb.velocity = new Vector3(0f, JumpPower, 0f);
         }
         isGround = false;
     }
 
     public bool AttemptFire() 
     { 
         if (Input.GetMouseButtonDown(0) && isReady && isGround)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.transform.tag == "Target")
                 {
                     Debug.Log("Fire Called by " + transform.name);
                     obj = hit.transform.name.ToString();
                     Fire(); 
                  // If we did fire
                     return true;
                 }
             }
         } 
      // If we didn't
      return false;
     }
     
     void Update()
     {
        
     }
 }

 public class GameManage : MonoBehaviour
 {
 
     private int playernumber;
     private PlayerController a, b, c,active;
     
     public void InitialAssign()
     {
         playernumber = 1;
         a = GameObject.Find("Player (1)").GetComponent<PlayerController>();       
         b = GameObject.Find("Player (2)").GetComponent<PlayerController>();
         c = GameObject.Find("Player (3)").GetComponent<PlayerController>();
         TurnDecider();
     }
 
     void Start()
     {
         InitialAssign();
     }
 
     public void PlayerSelector()
     {
         if (playernumber == 3)
         {
             playernumber = 1;
         }
         else
         {
             playernumber++;
         }
         TurnDecider();
     }
 
     public void TurnDecider()
     {
         switch (playernumber)
         {
             case 1:
                 a.isReady=true;                      
                 b.isReady = false;
                 c.isReady = false;
                 active=a;
                 Debug.Log("Player 1 is ready!");
                 break;
 
             case 2:
                 a.isReady = false;
                 b.isReady = true;                
                 c.isReady = false;
                 active=b;
                 Debug.Log("Player 2 is ready!");
                 break;
 
             case 3:
                 a.isReady = false;
                 b.isReady = false;
                 c.isReady = true;
                 active=c;
                 Debug.Log("Player 3 is ready!");
                 break;
         }
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0)&&active.AttemptFire())
         {           
             PlayerSelector();
         }
     }
 }
 


Comment
Add comment · Show 2 · 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
avatar image TreyH · Apr 06, 2018 at 06:03 PM 0
Share

If it's doing what it's supposed to do and you / your $$anonymous$$m think it's readable, then it works just fine.

avatar image degeta TreyH · Apr 06, 2018 at 06:14 PM 0
Share

I dont work with a $$anonymous$$m. I'm doing all this alone and I'm a beginner level unity developer. Just one year experience :) Anyways thanks for your support !

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity hub help 0 Answers

Ar foundation Face Tracking 0 Answers

how to download open jdk,sdk, ndk recommended for unity Unity 2019.3.1, without unity hub. 1 Answer

I cant get the basic FollowPlayer script to go to Main Camera in the hierarchy 0 Answers

I want to exactly put my player where i want it be. 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