【问题标题】:Unity collider touch not act on playerUnity collider touch 不作用于玩家
【发布时间】:2020-09-23 20:33:55
【问题描述】:

我有 playerground 与 2D 对撞机,玩家应该停在 ground 顶部,但它停在地面底部。

代码

PlayerController

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

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;
    private float moveSpeedStore;
    public float speedMultiplier;
    public float speedIncreateMilestone;
    private float speedIncreateMilestoneStore;
    private float speedMilestoneCount;
    private float speedMilestoneCountStore;
    public float jumpForce;
    public float jumpTime;
    private float jumpTimeCounter;
    private bool stoppedJumping;
    private bool canDoubleJump;
    private Rigidbody2D myRigidbody;
    public bool grounded;
    public LayerMask whatIsGround;
    public Transform groundCheck;
    public float groundCheckRadius;
    // private Collider2D myCollider;
    private Animator myAnimator;
    public GameManager theGameManager;
    public AudioSource jumpSound;
    public AudioSource deathSound;

    // Start is called before the first frame update
    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();

        jumpTimeCounter = jumpTime;
        speedMilestoneCount = speedIncreateMilestone;
        moveSpeedStore = moveSpeed;
        speedMilestoneCountStore = speedMilestoneCount;
        speedIncreateMilestoneStore = speedIncreateMilestone;
        stoppedJumping = true;
    }

    // Update is called once per frame
    void Update()
    {
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

        if(transform.position.x > speedMilestoneCount)
        {
            speedMilestoneCount += speedIncreateMilestone;
            speedIncreateMilestone = speedIncreateMilestone * speedMultiplier;
            moveSpeed = moveSpeed * speedMultiplier;
        }

        myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);

        if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) )
        {
            if(grounded)
            {
                myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
                stoppedJumping = false;
                jumpSound.Play();
            }

            if(!grounded && canDoubleJump)
            {
                myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
                jumpTimeCounter = jumpTime;
                stoppedJumping = false;
                canDoubleJump = false;
                jumpSound.Play();
            }
        }

        if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
        {
            if(jumpTimeCounter > 0)
            {
                myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
                jumpTimeCounter -= Time.deltaTime;
            }
        }

        if(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
        {
            jumpTimeCounter = 0;
            stoppedJumping = true;
        }

        if(grounded)
        {
            jumpTimeCounter = jumpTime;
            canDoubleJump = true;
        }

        myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
        myAnimator.SetBool("Grounded", grounded);
    }

    void OnCollisionEnter2D(Collision2D other) {
        if(other.gameObject.tag == "killbox")
        {
            theGameManager.RestartGame();
            moveSpeed = moveSpeedStore;
            speedMilestoneCount = speedMilestoneCountStore;
            speedIncreateMilestone = speedIncreateMilestoneStore;
            deathSound.Play();
        }
    }
}

Player settings

问题

我应该怎么做才能让我的播放器保持在地面上?

注意:这是我在unity 上的第一个问题,所以如果您需要任何类型的代码或数据,请尽管询问,我会尽力为您提供所需的。

谢谢。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    看起来你的对撞机太高了。您会注意到指示对撞机大小和位置的绿色框正在与地面上的正确位置发生碰撞。它的底部只是接触地面的顶部。您只需将碰撞器向下移动到播放器上,使绿色框的底部位于播放器精灵的底部。

    【讨论】:

    • 谢谢,普通的对撞机就像这张图片ibb.co/58kvmLV 但不知何故,当玩家接触地面时它会滑出
    • 但是盒子碰撞器有y offset = 1,我们可以看到截图。把它设为 0 再试一次?
    • 这与我尝试过的结果相同 0 -0.91 以及更多 ibb.co/PjQtYXq
    • 你能同时选择地面和玩家对象然后截图吗?在播放模式下也可以这样做。并向我们​​展示地面物体上的对撞机设置。
    • 给你select player and groundibb.co/qRtnrYMselect in play modeibb.co/1mzzKPxground collideribb.co/Sc9BNJ0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多