简体   繁体   English

Unity 2D:为什么角色不停地穿过其他几何图形?

[英]Unity 2D: Why does my character move through other geometry without stopping?

Whenever using A or D my character (2 dimensional game object) is not stopped by the other game objects. 每当使用A或D时,其他游戏对象都不会停止我的角色(二维游戏对象)。 The character has a character controller component attached as well as an animation controller with 3 animations (Idle, Walk and Land). 角色具有附加的角色控制器组件以及具有3种动画(空闲,行走和着陆)的动画控制器。

//Variables
public float speed = 10F;
public float jumpSpeed = 15F; 
public float gravity = 20F;
public float airSpeed = 5F;
public Vector2 moveDirectionResultant = Vector2.zero;
private CharacterController controller;
private Animator animator;


void Start() {

    controller = GetComponent<CharacterController>();
    animator = GetComponent<Animator>();
}

void Update() {

    animator.SetFloat ("AirSpeed", moveDirectionResultant.y);

    if (controller.isGrounded) 
    {
        animator.SetBool("Grounded", true);
        animator.SetBool("Move", false);

                    if (Input.GetKeyDown (KeyCode.W)) 
                    {
            moveDirectionResultant.y = jumpSpeed;
                    }

                    else
                    {
            moveDirectionResultant.y =  0;
                    }


                    if (Input.GetKey (KeyCode.D)) 
                    {
            transform.Translate(speed * Time.deltaTime, 0f, 0f);
            animator.SetBool("Move", true);
                    }

                    if (Input.GetKey (KeyCode.A)) 
                    {
            transform.Translate(-speed * Time.deltaTime, 0f, 0f);
            animator.SetBool("Move", true);
                    }

    }

    else 
    {
        animator.SetBool("Grounded", false);
        animator.SetBool("Move", false);

                if (Input.GetKey (KeyCode.D)) 
                {
            transform.Translate(airSpeed * Time.deltaTime, 0f, 0f);;
                }

                if (Input.GetKey (KeyCode.A)) 
                {
            transform.Translate(-airSpeed * Time.deltaTime, 0f, 0f);;
                }



    }



    //Applying gravity to the controller
    moveDirectionResultant.y -= gravity * Time.deltaTime;
    //Making the character move
    controller.Move(moveDirectionResultant * Time.deltaTime);
}

} }

I am relatively new to coding, so if the solution is obvious, I'm sorry! 我对编码还比较陌生,因此,如果解决方案显而易见,对不起!

Try attaching a RigidBody2D and BoxCollider2D to your gameobject, as well as to any gameobjects which you would like to collide with. 尝试将RigidBody2DBoxCollider2D到您的游戏对象以及想要与之碰撞的任何游戏对象。 If you'd like to have the collision cause an event to occur (a method to be called) use the OnCollisionEnter2D(Collision2D collision) method within a script attached to the gameobject. 如果您希望发生碰撞导致事件发生(称为方法),请使用附加在游戏对象上的脚本中的OnCollisionEnter2D(Collision2D collision)方法。 This tutorial video from unity's website goes into detail about 2d character control and animation: http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers 来自unity网站的本教程视频详细介绍了2D角色控制和动画: http//unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

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

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