实现预制Unity3D

时间:2016-05-07 19:06:20

标签: c# unity3d unityscript unity5 unity3d-2dtools

我有5个预制作为玩家选项,在我的游戏菜单上有一个“更改”按钮,用户可以点击,更改角色。

当game2d开始时,如何在“菜单”中进行此选择?

预制件:

Bee,Bee1,Bee2,Bee3和Bee4。

蜜蜂(玩家)剧本。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;

public class Bee : MonoBehaviour {

    public float moveSpeed;

    public Transform bee;
    private Animator animator;

    public bool isGrounded = true;
    public float force;

    public float jumpTime = 0.1f;
    public float jumpDelay = 0.1f;
    public bool jumped = false;
    public Transform ground;


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

    void Update ()
    {
        Move ();

    }


    void Move ()
    {

        isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor"));
        animator.SetFloat ("runB", Mathf.Abs (CrossPlatformInputManager.GetAxis ("Horizontal")));
        if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") > 0) {

            transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 0);
        }
        if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") < 0) {

            transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 180);
        }

        if (CrossPlatformInputManager.GetButtonDown ("Vertical") && isGrounded && !jumped) {

            //  rigidbody2D.AddForce (transform.up * force);
            GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
            jumpTime = jumpDelay;
            animator.SetTrigger ("jumpB");
            jumped = true;
        }

        jumpTime -= Time.deltaTime;

        if (jumpTime <= 0 && isGrounded && jumped) {

            animator.SetTrigger ("groundB");
            jumped = false;

        }

    }

}   

2 个答案:

答案 0 :(得分:0)

也许应该激活你需要的那个并停用所有其余的:

That tutorial can help you to do that

答案 1 :(得分:0)

我建议将播放器的可视部分移动到子对象中,但这不是必需的。如果你这样做,你的根播放器对象就可以成为一个空的游戏对象。

如果您有场景更改,则需要在场景之间保持不变的内容,例如:一个DontDestroyOnLoad的单身人士。无论如何,这必须包含有关所有可能的精灵的信息,例如:在您使用Resources.Load/LoadAll填充的列表中。您可以使用此列表在菜单中显示精灵。选择后你保存索引,在游戏开始时你只需用这样的东西将玩家精灵更改为选定的精灵(这将在这个类中,例如称为GameController):

playerGO.GetComponentInChildren<SpriteRenderer>().sprite = spritesList[selected];

(假设视觉效果在子对象上。)