简体   繁体   中英

How To Allow Player To Move In Front And Behind Objects In Unity

I'm making a game using Unity 3D in the style of 2D. The camera only follows the player left to right but I want my environment to have a feeling of depth. This isn't a top down game, it's more like a 2d mario platformer game. I want the player to be able to move in front of objects, so that a tree for example would be behind them when they press down and if they press up to move further back into the environment so that the tree is now in front of them.

This is my movement script so far.

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

    Animator anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
        Movement ();
    }
    void Movement(){

        anim.SetFloat("Speed", Mathf.Abs (Input.GetAxisRaw ("Horizontal")));

        if (Input.GetAxisRaw ("Horizontal") > 0) {
            transform.Translate(Vector2.right* 4f*Time.deltaTime);
            transform.eulerAngles = new Vector2(0, 0); 
        }
        if (Input.GetAxisRaw ("Horizontal") < 0) {
            transform.Translate (Vector2.right * 4f * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 180);
        }

        if(Input.GetAxisRaw("Vertical") >0){
            transform.Translate (Vector2.up * 4f * Time.deltaTime);
            transform.eulerAngles = new Vector2(0, 0);
    }

        if(Input.GetAxisRaw("Vertical") <0){
            transform.Translate (Vector2.down * 4f * Time.deltaTime);
            transform.eulerAngles = new Vector2(0, 0); 
        }
    }
}

Sounds like you need to change the sort order for your sprites so that when the player is above a tree, the player sprite sorts behind it. Try adding something like this to the update() of a component on your player and all your trees.

var sprite = GetComponent<SpriteRenderer>();
sprite.sortingOrder = Mathf.RoundToInt(transform.position.y * -10f);

This method works best if you set the pivot of the sprites to where they touch the ground when you import them.

The solution really depends on how you're doing your level.

If everything is on the same lane, so to speak, then modifying the sprite renderers sorting order could provide you with the solution. Check the sprite renderer documentation: https://docs.unity3d.com/Manual/class-SpriteRenderer.html

Now comes the challenge, if everything is on separate layers. By layers I mean the sorting layer that you provide the sprite renderer. This can yield some interesting results, as simply modifying the sorting order won't be enough, you'll have to change the layer as well. You'll changing the layer to the one above it in your project settings, if I'm not mistaken. If everything is on the same layer though, then changing the sorting order would be more than enough.

Don't forget to consider potential issues with collision detection as well, as changing the lane of the game object will mean that you may have to change its physics layer to, so the colliders don't hit anything on the layer in front. https://docs.unity3d.com/Manual/LayerBasedCollision.html

Hopefully this leads you in the right direction.

One solution is simply changing the Z axis value of the position of your player. My understanding is that with a camera pointing in the +Z direction, sprites with a lower Z value will be positioned in front of others.

By changing the Z value of your player will change the order in which they are drawn, thus making it appear going behind or in front of certain elements. For this to work, your level should be built with the environment positioned at different values in the Z axis, of course.

if (Input.GetKeyDown (KeyCode.UpArrow)) {
        transform.Translate (new Vector3 (0, 0, 1));
}
if (Input.GetKeyDown (KeyCode.DownArrow)) {
        transform.Translate (new Vector3 (0, 0, -1));
}

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