Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

PONG Game

Discussion in '2D' started by RunOrDi33, Sep 7, 2019.

  1. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    Hi all , i have a stupid problem i know but Please i need help ! :( , i coded a Computer player in my game and the code is (
    using UnityEngine;
    using System.Collections;

    public class NPC : MonoBehaviour {

    private Transform myTransform ;

    public GameObject Ball ;

    private float vert ;

    private int skill = 2;

    public GameObject DeadNPC ;

    void Start (){

    myTransform = transform;

    myTransform.position = new Vector3(9.5f,vert,-2);

    }

    void Update (){

    vert = Ball.transform.position.y;

    myTransform.position = new Vector3(9.5f,vert,-2);

    Vector3 position = new Vector3(myTransform.position.x,myTransform.position.y,myTransform.position.z);

    if(skill < 1){

    Instantiate(DeadNPC,position,Quaternion.identity);
    Destroy(gameObject);
    }

    }

    void OnTriggerEnter ( Collider collider ){

    if(collider.CompareTag("Ball")){
    skill --;
    }
    }
    }
    the Computer moves fine with Ball but when the skill comes under 1 , the NPC don't turn to DeadNPC and im sure 100% i attached the DeadNPC into NPC Script... Please Could someone Help me ?
     
  2. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    Instantiate(deadNPC, transform.position, transform.rotation)
     
  3. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    didn't work :(
     
  4. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    But the gameObject gets destroyed?, cause it isnt maye be cause a counter bug
     
  5. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    no it didn't , sorry for my late answer ? could u help me to make a ai computer player ? Please
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,453
    Throw in some Debug.Logs in there to see what is being called and what isnt. Put one in the OnTriggerEnter (note, if you are doing 2D, it needs to be OnTriggerEnter2D to work) that logs the value of "skill" and then put another under the IF condition when skill becomes less than 1.
     
  7. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    i changed OnTriggerEnter to 2d and same problem :( also i added debug.log and nothing appears ! in consol
     
    Last edited: Sep 18, 2019
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,453
    So keep it as OnTriggerEnter2D since it is a 2D game. Make sure your variable in the argument changes to 2D as well:
    void OnTriggerEnter2D(Collider2D col). So if there is no console log for if skill is less than 1 you know something is wrong with your condition. Have you checked to see if skill ever gets to be below 1? Does skill ever lower at all?
     
  9. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    its the whole Code :-

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player_2_Controls : MonoBehaviour
    {
    private Transform myTransform;

    public GameObject Ball;

    private float vert;

    private int skill = 2;

    public GameObject DeadNPC;

    void Start()
    {

    myTransform = transform;

    myTransform.position = new Vector3(7.5f, vert, -2);

    }

    void Update()
    {

    vert = Ball.transform.position.y;

    myTransform.position = new Vector3(7.5f, vert, -2);

    Vector3 position = new Vector3(myTransform.position.x, myTransform.position.y, myTransform.position.z);

    if (skill < 1)
    {
    Instantiate(DeadNPC, transform.position, transform.rotation);
    Destroy(gameObject);
    }

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
    if (collision.CompareTag("Ball"))
    {
    skill--;
    }
    }
    }
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,453
    Use code tags please if you are going to keep posting long bits of code. Then i can really help. Edit your last post and change it. upload_2019-9-19_8-13-48.png
     
    Helladah likes this.
  11. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    yea please, use code tags
     
  12. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    Code (CSharp):
    1. public class Computer_Ai : MonoBehaviour
    2. {
    3.     public float movementSpeed;
    4.     public GameObject ball;
    5.  
    6.     private void FixedUpdate()
    7.     {
    8.         if (Mathf.Abs(this.transform.position.y - ball.transform.position.y) > 50)
    9.         {
    10.             if(transform.position.y < ball.transform.position.y)
    11.             {
    12.                 GetComponent<Rigidbody2D>().velocity = new Vector2(0, 1) * movementSpeed;
    13.             }
    14.             else
    15.             {
    16.                 GetComponent<Rigidbody2D>().velocity = new Vector2(0, -1) * movementSpeed;
    17.             }
    18.         }
    19.         else
    20.         {
    21.             GetComponent<Rigidbody2D>().velocity = new Vector2(0,0);
    22.         }
    23.     }
    24. }
     
  13. RunOrDi33

    RunOrDi33

    Joined:
    Mar 15, 2019
    Posts:
    42
    i tryed with that code but the racket didn't move with ball , i dunno why .. didn't get any error ... sorry for late answer i was out of my home ... i dunno why the racket here did't move , so may you check this Please ?
     
    Last edited: Oct 3, 2019
  14. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,453
    Did you set the movement speed in the inspector?
     
  15. DigitalGamezStudio

    DigitalGamezStudio

    Joined:
    Oct 4, 2019
    Posts:
    8
    Just a little tip that I practice use public to configure the correct speed then set private and manual set the speed ;)