简体   繁体   中英

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. The character has a character controller component attached as well as an animation controller with 3 animations (Idle, Walk and Land).

//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. 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. 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

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