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 /
avatar image
0
Question by Bradybeast128 · Sep 25, 2018 at 06:19 AM · controllergravitynot workinggolfco-op

Player 2 doesn't respond

I'm making a co-op / single player golf game with gravity physics and for some reason, player 1 works perfectly fine while the controller for player 2 doesn't work at all except for camera zoom. Here is both the camera controller and the player script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraControl : MonoBehaviour {
     public GolfBall golf; //script that is attached to the gameobject that allows the ball to move
     public Camera cam; //The camera that is attached to the golf ball;
     public bool Player2; // The same as player 2 in the GolfBall script, I just though it would help to have it be a local bool
     void Start()
     {
     
     }
     // Update is called once per frame
     void Update () {
         cam = GetComponentInChildren<Camera>();
         golf = GetComponent<GolfBall>();
         Player2 = golf.player2;
         //The basic camera controls for player 1
         if (Player2 == false)
         {
             if (Input.GetKey(KeyCode.A))
             {
                 transform.Rotate(Vector3.up * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.D))
             {
                 transform.Rotate(Vector3.down * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.W))
             {
                 transform.Rotate(Vector3.left * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.S))
             {
                 transform.Rotate(Vector3.right * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.Q))
             {
                 transform.Rotate(Vector3.forward * 70 * Time.deltaTime);
             } 
             if (Input.GetKey(KeyCode.E))
             {
                 transform.Rotate(Vector3.back * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.F))
             {
                 cam.fieldOfView -= Time.deltaTime * 70;
             }
             if (Input.GetKey(KeyCode.R))
             {
                 cam.fieldOfView += Time.deltaTime * 70;
             }
             
         }
         //The basic camera controls for player 2
         if (Player2 == true)
         {
             if (Input.GetKey(KeyCode.Keypad4))
             {
                 transform.Rotate(Vector3.up * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.Keypad6))
             {
                 transform.Rotate(Vector3.down * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.Keypad8))
             {
                 transform.Rotate(Vector3.left * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.Keypad5))
             {
                 transform.Rotate(Vector3.right * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.Keypad7))
             {
                 transform.Rotate(Vector3.forward * 70 * Time.deltaTime);
             } 
             if (Input.GetKey(KeyCode.Keypad9))
             {
                 transform.Rotate(Vector3.back * 70 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.KeypadEnter))
             {
                 cam.fieldOfView -= Time.deltaTime * 70;
             }
             if (Input.GetKey(KeyCode.KeypadPlus))
             {
                 cam.fieldOfView += Time.deltaTime * 70;
             }
         }
     }
 }
 

and the character controller:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class GolfBall : MonoBehaviour {
     public Slider power; //How much power is being forced onto the rigidbody
     public GameObject slider; //The gameobject attached to the sider power
     public bool inair; //If the gameobject has a collision with a planet, this is false
     private Rigidbody r; // the rigidbod attached to this Gameobject
     private Camera c; // the camera that is a child of this gameobject
     public int score; // How many times the ball has been hit
     public float cooldown; // the cooldown between swings
     public bool player2; //Is this player 2?
     public Text te; // the text that shows the player's score
     public bool finished; // has this object hit the flag?
     
     // Use this for initialization
     void Start () 
     {
         slider = power.gameObject;
         r = GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update()
     {
         if (finished == true)
         {
             this.enabled = false;
             // if this object has hit the flag, then disable this compontent so the player cant move
         }
         else
         {
             cooldown -= Time.deltaTime;
             if (player2 == false)//is this player 2?
             {
                 //The text shows the player score
                 te.text = "Player 1 : " + score;
                 if (Input.GetKey(KeyCode.X))
                 {
                     //if the player presses the x key, the power goes down
                     power.value -= Time.deltaTime * 1000;
                 }
                 if (Input.GetKey(KeyCode.Z))
                 {
                     //if the player presses the z key, the power goes up
                     power.value += Time.deltaTime * 1000;
                 }
             }
             
             if (player2 == true)//is this player 2?
             {
                 //The text shows the player score
                 te.text = "Player 2 : " + score;
                 if (Input.GetKey(KeyCode.Keypad2))
                 {
                     //if the player presses the keypad 2, the power goes down
                     power.value -= Time.deltaTime * 1000;
                 }
                 if (Input.GetKey(KeyCode.Keypad1))
                 {
                     //if the player presses the keypad 1, the power goes up
                     power.value += Time.deltaTime * 1000;
                 }
             }
             if (inair == false) // is the current gameobject in air?
             {
                 //if not, then the slider is active
                 slider.SetActive(true);
             }
             else
             {
                 //if it is, than set the power slider to inactive and inair to true
                 slider.SetActive(false);
                 inair = true;
             }
         }
     }
     void Up()
     {
         if (cooldown <= 0) // if cooldown is less than zero
         {
             if (player2 == false) // is this player 2?
             {
                 if (Input.GetKeyDown(KeyCode.Space))//if player 1 hit the spacebar, than add 1 to score, make cooldown equal 1, and add force to the rigidbody times the value of power slider
                 {
                     score += 1;
                     cooldown = 1;
                     inair = true;
                     r.AddForce(transform.forward * power.value);
                 }
                 else
                 {
                     r.AddForce(Vector3.zero);
                 }
             }
             if (player2 == true)
             {
                 if (Input.GetKeyDown(KeyCode.RightShift))//if player 2 hits rightshift, than add 1 to score, make cooldown equal 1, and add force to the rigidbody times the value of power slider
                 {
                     score += 1;
                     cooldown = 1;
                     inair = true;
                     r.AddForce(transform.forward * power.value);
                 }
                 else
                 {
                     r.AddForce(Vector3.zero);
                 }
             }
         }
     }
     void OnCollisionStay(Collision other) // if it detects a collision
     {
             r.AddForce(Vector3.zero);
             if (other.gameObject.tag == "Planet")//if the collision is with a planet, than inair equals false and use the up function
             {
                 inair = false;
                 Up();
             }
             else
             {
                 inair = true;
             }
             if (other.gameObject.tag == "Boundry")// WIP out of bounds
             {
                 score += 3;
             }
     }
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "Flag") //if the gameobject hits the flag, finished is true
         {
             finished = true;
         }
         if (other.gameObject.tag == "Atmosphere")//atmospheric drag
         {
             r.drag = 5;
         }
         if (other.gameObject.tag == "BlackHole") 
         {
            //i dont know what im going to put here yet
         }
     }
 }

Any help is appreciated. Thank you!

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

160 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gravity not working with character control script 1 Answer

Allowing player to glide/fall slowly 2 Answers

Trouble carrying over scripted gravity to NPC. 0 Answers

Gravity stop working after adding 2nd layer 0 Answers

Changing gravity at the click of a button... 5 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