简体   繁体   English

在统一乒乓球游戏中触发桨不跟随桨

[英]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此外,如果您想正确检测碰撞,请使用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.只需将BoxCollider2D组件附加到可见桨并从那里检测碰撞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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