Search Unity

Movement script controlling both objects it's attached to when it should not be

Discussion in 'Getting Started' started by itshirb_, Jan 10, 2022.

  1. itshirb_

    itshirb_

    Joined:
    Jan 3, 2022
    Posts:
    30
    Hi, so I'm pretty new to Unity and C# as a whole, and as my first project I'm attempting to recreate pong, and I'm having trouble with one of my scripts.
    Now as I said I'm new to both Unity and C# so this approach probably isn't ideal or that good but basically I'm trying to have one script control all paddle related movement, and I'm trying to make this movement script behave differently based off which paddle it's on.
    Code (CSharp):
    1. // Paddle script for pong
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Paddle : MonoBehaviour
    8. {
    9.     public bool Player = true; // checks if paddle is being controlled by player
    10.     public int controller = 0; // checks if paddle is player one or player two
    11.     public float speed = 7f; // control paddle speed
    12.  
    13.     // Set in the editor, the way I understand it is this SHOULD be
    14.     // making each instance of this script control a different paddle.
    15.     // The issue is having this script on both paddles makes both move with W/S & UP/DOWN.
    16.     // Obviously yes i could probably make a second script and use that for the second paddle
    17.     // but I'd rather have all paddle related controls in one script.
    18.     public GameObject activePlr = null;
    19.  
    20.     void Start()
    21.     {
    22.         // afaik nothing to do here
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         // check if player
    28.         if (Player == true)
    29.         {
    30.             // check if player one
    31.             if (controller == 0)
    32.             {
    33.                 if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
    34.                 {
    35.                     activePlr.transform.position = activePlr.transform.position + new Vector3(0, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0);
    36.                 }
    37.                 if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
    38.                 {
    39.                     activePlr.transform.position = activePlr.transform.position - new Vector3(0, Input.GetAxis("Vertical") * -speed * Time.deltaTime, 0);
    40.                 }
    41.             }
    42.             // check if player two
    43.             else if (controller == 1)
    44.             {
    45.                 if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
    46.                 {
    47.                     activePlr.transform.position = activePlr.transform.position + new Vector3(0, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0);
    48.                 }
    49.                 if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
    50.                 {
    51.                     activePlr.transform.position = activePlr.transform.position - new Vector3(0, Input.GetAxis("Vertical") * -speed * Time.deltaTime, 0);
    52.                 }
    53.             }
    54.         }
    55.         // boundaries
    56.         if (transform.position.y > 4.4f)
    57.         {
    58.             transform.position = new Vector3(transform.position.x, 4.4f, transform.position.z);
    59.         }
    60.         if (transform.position.y < -4.4f)
    61.         {
    62.             transform.position = new Vector3(transform.position.x, -4.4f, transform.position.z);
    63.         }
    64.     }
    65. }
    From what I understand, this should result in both paddle's movements being separate from each other, with W/S controlling player one's paddle, and UP/DOWN controlling player two's paddle, but instead all 4 keybinds seem to be controlling both paddles, as pressing W and DOWN results in either both not moving or depending on which was pressed first, moving up or down but very slowly.

    This is all very new and confusing to me so any help would be greatly appreciated, as well as any general coding tips to help improve the script.