简体   繁体   中英

Trigger paddle not following paddle in unity pong game

The only way I know to detect collision is through a trigger. Therefor, I have a paddle hitting the ball and a trigger to keep the score. However, for some reason the trigger paddle won't folloWhen I move up, the trigger paddle follows behind the normal paddle slightly then returns to it's initial position. Here is a picture of the paddle bar, the trigger is inside the normal paddle. 在此处输入图片说明

and here is the code to move the paddle, they have the same script but the trigger won't act correctly. Also note, that it says in the position bar on the right of the image for the trigger paddle that it is following the paddle. But it is not

     void Start () {
    dimensions = new Vector3(transform.localScale.x, transform.localScale.y, 0);
    transform.localScale = new Vector3(.5f, .5f, 1);
}

// Update is alled once per frame
void Update () {
    yPos = gameObject.transform.position.y + (Input.GetAxis ("Vertical") * paddleSpeed);

    if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
        playerPos = new Vector3 (gameObject.transform.position.x, Mathf.Clamp(yPos, -yClamp, yClamp), 0);

    gameObject.transform.position = playerPos;
}

I can't figure out why it won't work. Any help is extremely appreciated seeing as I have no help looking online and can't figure it out.

If you simply want the 'trigger paddle' to follow the position of the visible paddle then put the trigger paddle as a child of the visible paddle in the heirarchy (It looks like you've done this already). Nothing else is needed. Remove script you have assigned to make the trigger paddle move in the same way that the regular paddle does.

also, if you want to detect a collision properly then use OnCollisionEnter2D

also also, I know you most likely just want to get this working, but this can be done in a much better way. You don't need the child object at all. Just attach the BoxCollider2D component to the visible paddle and detect the collision from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM