2d波浪效果 unity3d
2014-11-07 15:51:57 jianwei954 阅读数 396

官方地址:www.unity3d.com

下载完安装时需要注册账号,安装时可以选择收费版本和免费版本(部分功能不能使用)。

原文:http://docs.unity3d.com/Manual/Overview2D.html 部分百度翻译,太拗口了。

Gameplay in 2D


2D视角模式:

虽然Unity出名的是它的3D能力,但是它同样能用于2D游戏的开发。在没有其他附加插件的情况下强大的编辑器功能适用于2D游戏的简化开发。

最引人瞩目的功能是它的2D视图模式按钮,它位于Scene视图面板的工具条上。当2D模式选中时,摄像机相当于设置为正射(平射视角),摄像机看起来处于Z轴和Y轴的增量方向上。这使你布置2D场景更加的容易。

2D图形:

二维图形对象被称为精灵。精灵本来只是标准的纹理,但是通过发展后有特殊的方法来结合和管理精灵纹理的性能和效果。Unity提供了一个内置的精灵编辑器来让你从一个大图像中提取图形精灵。这允许您在修改很多组件图形时只需要修改一个单一的纹理就可以了。例如使角色的手臂腿和身体区分开来

精灵被精灵渲染器组件渲染效果好于网格渲染器使用的3D对象。你可以添加到一个游戏对象通过组件菜单(组件>渲染>精灵渲染器,或者,你可以创建一个游戏对象直接与一个精灵渲染器已经连接(菜单:对象>创建其他>精灵)(Component > Rendering > Sprite Renderer or alternatively, you can just create a GameObject directly with a Sprite Renderer already attached (menu: GameObject > Create Other > Sprite)。

2D物理
Unity有独立的物理引擎来处理2D物理效果,但是要利用优化的2D才可用。组成部分对应标准的3D物理组件如刚体,盒子碰撞器和铰链,但对于2D要贴上“2D”的标签。因此,精灵可以配备刚体2D,盒子碰撞器2D和铰链的2D。大多数2D物理组件是简单的“扁平化”的3D等值的版本(例如,盒子碰撞器2D是方形而盒子碰撞器是一个立方体)但也有少数的例外。

2017-04-17 09:22:10 qq_28129713 阅读数 157

//音频

using UnityEngine;

using System.Collections;

public class GameController:MonoBehaviour
{
    private AudioClip baoshi_sound;
    private AudioClip jump_sound;
    private AudioClip dead_sound;
    private AudioSource soundSource;
    private static GameController instance = null;
    public AudioSource soundMgr;

    public static GameController Instance
    {
        get
        {
            return instance;
        }
    }
    void Awake()
    {
        instance = this;
        soundSource = gameObject.GetComponent<AudioSource>();
        baoshi_sound = Resources.Load("Sounds/baoshi_eat") as AudioClip;
        dead_sound = Resources.Load("Sounds/dead") as AudioClip;
        jump_sound = Resources.Load("Sounds/jump") as AudioClip;
    }
    public void Playbaoshi_sound()
    {
        soundSource.clip = baoshi_sound;
        soundSource.Play();

    }
    public void Playdead_sound()
    {
        soundSource.clip = dead_sound;
        soundSource.Play();
    }
    public void Playjump_sound()
    {
        soundSource.clip = jump_sound;
        soundSource.Play();
    }

}

//panel切换

using UnityEngine;
using System.Collections;

public class Panel_start : MonoBehaviour
{
    public GameObject panel_start;
    public GameObject panel_main;
    void Start ()
    {

    }

    void Update ()
    {

    }
    public void OnClick()
    {
        panel_start.SetActive(false);
        
        panel_main.SetActive(true);
        Application.LoadLevel(0);
    }
}

//模拟屏幕控制

using UnityEngine;
using System.Collections;

public class HUDLayer : MonoBehaviour
{
    public Vector2 zhongli = new Vector2(0, -20);
    public Vector2 click = new Vector2(0, 12);
    public Vector2 tap = new Vector2(0, 20);
    public Rigidbody2D body;
    public bool start = false;
    public float speed = 0.09f;

    bool StartCountTime = false;
    float totalTime;
    int speed_y = 1;

    //public Animation Jump;
    public GameObject Role_Jump;

    public GameController jumpSource;
    public void OnPointerDown()
    {
        StartCountTime = true;

    }
    public void OnPointerUp()
    {
        StartCountTime = false;
        if (totalTime < 0.15&&speed_y==0)
        {
            body.velocity = click;
            //animation["Jump"].speed = 0;
            body.transform.Find("role_run").GetComponent<Animator>().speed = 0;
            jumpSource.Playjump_sound();
        }
        totalTime = 0;
    }
    void Start ()
    {
        jumpSource = GameObject.Find("SourceManger").GetComponent<GameController>();
    }

    void Update ()
    {
        
        float a=speed + PlayerPrefs.GetInt("floors_num")*0.0005f;
        if (PlayerPrefs.GetInt("stop") == 1)
        {
            a = 0;
            click = new Vector2(0, 0);
            tap = new Vector2(0, 0);
        }
        if (body.velocity.y < 0.2 && body.velocity.y > -0.2)
        {
            speed_y = 0;
        }
        else
        {
            speed_y = 1;
        }
        body.AddForce(zhongli);
        body.transform.Translate(a, 0, 0);
        if (StartCountTime)
        {
            totalTime += Time.deltaTime;
            if (0.2 > totalTime && totalTime > 0.15 &&speed_y==0)
            {
                
                body.velocity = tap;
                body.transform.Find("role_run").GetComponent<Animator>().speed = 0;
                //Debug.Log("给力了");
                jumpSource.Playjump_sound();
            }
           

        }
    }
}

//人物运动

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine.UI;

public class Sport_role : MonoBehaviour
{

    Rigidbody2D body;
    //Rigidbody2D body_run;
    public GameObject body_run;
    public Transform[] leftTrans;
    public Transform[] rightTrans;
    public Transform[] blockTrans;
    public List<GameObject> start_TrapArray;
    public List<GameObject> TrapArray;
    public List<GameObject> Floor_color;
    public List<Text> Floors_txt;
    private GameObject baoshiModel;
    private GameObject baoshi_num_model;
    public GameObject blockPos;
    public GameObject MainPanel;
    public GameObject Jinghao;
    public int floor_start;
    public  int floors_num;
    public  static int floors_num_best;
    public static int baoshi_num;

    public GameObject panel_start;
    public GameObject panel_main;
    public Text floors_num_text;
    public Text floors_num_best_text;
    public Text baoshi_num_text;

    public Camera UIcamera;

    public GameController deadSource;
    public GameController baoshi_eat_Source;
    void Start ()
    {
        body = gameObject.GetComponent<Rigidbody2D>();
        floor_start = Random.Range(0, 8);
        start_TrapArray[floor_start].SetActive(true);
        Debug.Log(floor_start);

        PlayerPrefs.SetInt("stop", 0);
        deadSource = GameObject.Find("SourceManger").GetComponent<GameController>();
        baoshi_eat_Source = GameObject.Find("SourceManger").GetComponent<GameController>();
        //gameObject.GetComponent<Animator>().Stop();
    }

    void Update ()
    {
        floors_num_text.text = floors_num + "";
        floors_num_best_text.text ="纪录: "+ floors_num_best ;
        baoshi_num_text.text = baoshi_num + "";
        Floors_txt[0].text = floors_num + "";
        Floors_txt[1].text = floors_num + "";
        Floors_txt[2].text = floors_num + "";
        Floors_txt[3].text = floors_num + "";
        PlayerPrefs.SetInt("floors_num", floors_num);
        if (body.transform.localPosition.y >= 145)
        {
            Floors_txt[0].enabled = true;
            Floors_txt[1].enabled = false;
            Floors_txt[2].enabled = false;
            Floors_txt[3].enabled = false;
            //Floor_color[0].transform.do
            Floor_color[0].GetComponent<Image>().color = new Vector4(1, 1, 1, 1);
            Floor_color[1].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[2].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[3].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
        }else if(body.transform.localPosition.y<145&&body.transform.localPosition.y>43){
            Floors_txt[0].enabled = false;
            Floors_txt[1].enabled = true;
            Floors_txt[2].enabled = false;
            Floors_txt[3].enabled = false;
            Floor_color[0].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[1].GetComponent<Image>().color = new Vector4(1, 1, 1, 1);
            Floor_color[2].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[3].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
        }
        else if (body.transform.localPosition.y < 40 && body.transform.localPosition.y > -70)
        {
            Floors_txt[0].enabled = false;
            Floors_txt[1].enabled = false;
            Floors_txt[2].enabled = true;
            Floors_txt[3].enabled = false;
            Floor_color[0].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[1].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[2].GetComponent<Image>().color = new Vector4(1, 1, 1, 1);
            Floor_color[3].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
        }else if(body.transform.localPosition.y <=-70){
            Floors_txt[0].enabled = false;
            Floors_txt[1].enabled = false;
            Floors_txt[2].enabled = false;
            Floors_txt[3].enabled = true;
            Floor_color[0].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[1].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[2].GetComponent<Image>().color = new Vector4(1, 1, 1, 0.156f);
            Floor_color[3].GetComponent<Image>().color = new Vector4(1, 1, 1, 1);
        }
        if (body.velocity.y > 0.2 || body.velocity.y < -0.2)
        {
            //body_run.animation["New sprite"] = 0;
            //gameObject.GetComponent<Animator>().Stop();
        }
    }

    public int floor;
    public void OnTriggerEnter2D(Collider2D collision)
    {
        // int text_num;

        //gameObject.GetComponent<Animator>().speed = 0;
        
        if (collision.gameObject.CompareTag("left_wall"))
        {
            floors_num++;
            floors_num_best = floors_num_best > floors_num ? floors_num_best : floors_num;
            body.transform.Rotate(0, 180, 0);
           
            if (floor == 3 || floor == 11 || floor == 19 || floor == 27)
            {
                TrapArray[floor].transform.localPosition += new Vector3(0, 20, 0);
            }
            else if (floor == 4 || floor == 12 || floor == 20 || floor == 28)
            {
                
                TrapArray[floor].transform.Find("Image_down").localPosition -= new Vector3(0, 20, 0);
                TrapArray[floor].transform.Find("Image_up").localPosition += new Vector3(0, 20, 0);
            }
            else
            {
                TrapArray[floor].transform.localPosition -= new Vector3(0, 20, 0);
            }
            TrapArray[floor].SetActive(false);
            Destroy(baoshiModel);
            //Floors_txt[text_num].enabled = false;
        Found:
            int count_left = Random.Range(0, 4);
            //text_num = count_left;
            if (transform.localPosition.y < leftTrans[count_left].localPosition.y+90&&transform.localPosition.y>leftTrans[count_left].localPosition.y-10)
            {
                goto Found;
            }
            else
            {
                //Debug.Log(transform.localPosition.ToString());
                transform.localPosition = leftTrans[count_left].localPosition;
                
                //blockPos.transform.localPosition = transform.localPosition;
            }
            
            floor = count_left*8+Random.Range(0,8);
            //floor =  0;
            TrapArray[floor].SetActive(true);
            //Floors_txt[text_num].enabled = true;
            //TrapArray[floor].transform.DOLocalMove(new Vector3(TrapArray[floor].transform.localPosition.x, TrapArray[floor].transform.localPosition.y + 20, 0), 0.5f);
            if (floor == 3 || floor == 11 || floor == 19 || floor == 27)
            {
                TrapArray[floor].transform.DOLocalMove(new Vector3(TrapArray[floor].transform.localPosition.x, TrapArray[floor].transform.localPosition.y - 20, 0), 0.3f);
            }
            else if (floor == 4 || floor == 12 || floor == 20 || floor == 28)
            {
                //Debug.Log("@@" + TrapArray[floor].transform.FindChild("Image_down").gameObject.name);
                TrapArray[floor].transform.Find("Image_down").DOLocalMove(
                    new Vector3(TrapArray[floor].transform.Find("Image_down").localPosition.x, TrapArray[floor].transform.Find("Image_down").localPosition.y + 20, 0), 0.3f);
                TrapArray[floor].transform.Find("Image_up").DOLocalMove(
                    new Vector3(TrapArray[floor].transform.Find("Image_up").localPosition.x, TrapArray[floor].transform.Find("Image_up").localPosition.y - 20, 0), 0.3f);
            }
            else
            {
                TrapArray[floor].transform.DOLocalMove(new Vector3(TrapArray[floor].transform.localPosition.x, TrapArray[floor].transform.localPosition.y + 20, 0), 0.3f);
            }
            if (Random.Range(0, 10) > 5)
            {
                baoshiModel = (GameObject)Instantiate(Resources.Load("Prefabs/" + "baoshi"));
                baoshiModel.transform.parent = blockPos.transform;
                baoshiModel.transform.localPosition = new Vector3(88 - Random.Range(0, 4)*20, 230 - count_left * 100, 0);
                baoshiModel.transform.localScale = new Vector3(1, 1, 1);
            }
            //TrapArray[floor].transform.localPosition -= new Vector3(0,20,0);
            //baoshiModel = (GameObject)Instantiate(Resources.Load("Prefabs/" + "Block_1"));
            //baoshiModel.transform.parent = blockTrans[count];
            //baoshiModel.transform.localPosition = blockTrans[count].localPosition;
            //Debug.Log("222");
        }
        else if (collision.gameObject.CompareTag("right_wall"))
        {
            start_TrapArray[floor_start].SetActive(false);
            floors_num++;
            body.transform.Rotate(0, 180, 0);
            floors_num_best = floors_num_best > floors_num ? floors_num_best : floors_num;
            if (floor == 3 || floor == 11 || floor == 19 || floor == 27)
            {
                TrapArray[floor].transform.localPosition += new Vector3(0, 20, 0);
            }
            else if (floor == 4 || floor == 12 || floor == 20 || floor == 28)
            {

                TrapArray[floor].transform.Find("Image_down").localPosition -= new Vector3(0, 20, 0);
                TrapArray[floor].transform.Find("Image_up").localPosition += new Vector3(0, 20, 0);
            }
            else
            {
                TrapArray[floor].transform.localPosition -= new Vector3(0, 20, 0);
            }
            TrapArray[floor].SetActive(false);
            Destroy(baoshiModel);
            
            //Debug.Log("111");
        Found:
            int count = Random.Range(0, 4);
        if (transform.localPosition.y < rightTrans[count].localPosition.y + 90 && transform.localPosition.y > rightTrans[count].localPosition.y - 10)
            {
                goto Found;
            }
            else
            {
                transform.localPosition = rightTrans[count].localPosition;
                //Debug.Log(transform.localPosition.ToString());
                
            }
        floor = count * 8 + Random.Range(0, 8);
        //floor = count * 8 + 0;
        TrapArray[floor].SetActive(true);
        
        if (floor == 3 || floor == 11 || floor == 19 || floor == 27)
        {
            TrapArray[floor].transform.DOLocalMove(new Vector3(TrapArray[floor].transform.localPosition.x, TrapArray[floor].transform.localPosition.y - 20, 0), 0.3f);
        }
        else if (floor == 4 || floor == 12 || floor == 20 || floor == 28)
        {
            TrapArray[floor].transform.Find("Image_down").DOLocalMove(
                new Vector3(TrapArray[floor].transform.Find("Image_down").localPosition.x, TrapArray[floor].transform.Find("Image_down").localPosition.y + 20, 0),0.3f);
            TrapArray[floor].transform.Find("Image_up").DOLocalMove(
                new Vector3(TrapArray[floor].transform.Find("Image_up").localPosition.x, TrapArray[floor].transform.Find("Image_up").localPosition.y - 20, 0), 0.3f);
        }
        else
        {
            TrapArray[floor].transform.DOLocalMove(new Vector3(TrapArray[floor].transform.localPosition.x, TrapArray[floor].transform.localPosition.y + 20, 0), 0.3f);
        }
        if (Random.Range(0, 10) > 5)
        {
            baoshiModel = (GameObject)Instantiate(Resources.Load("Prefabs/" + "baoshi"));
            baoshiModel.transform.parent = blockPos.transform;
            baoshiModel.transform.localScale = new Vector3(1, 1, 1);
            baoshiModel.transform.localPosition = new Vector3(88 - Random.Range(0, 50), 230 - count * 100, 0);
        }
        }
        else if (collision.gameObject.CompareTag("baoshi"))
        {
            baoshi_num++;
            //Destroy(baoshiModel);
            baoshiModel.SetActive(false);
            baoshi_num_model = (GameObject)Instantiate(Resources.Load("Prefabs/" + "baoshi_num_model"));
            baoshi_num_model.transform.parent = blockPos.transform;
            baoshi_num_model.transform.localScale = new Vector3(1, 1, 1);
            baoshi_num_model.transform.localPosition = baoshiModel.transform.localPosition;
           // Destroy(baoshi_num_model, 0.5f);
            baoshi_eat_Source.Playbaoshi_sound();

        }
        else if (collision.gameObject.CompareTag("block"))
        {
            gameObject.transform.Find("role_run").GetComponent<Animator>().speed = 0;
            MainPanel.transform.GetComponent<DOTweenAnimation>().DOPlay();
            PlayerPrefs.SetInt("stop", 1);
            Jinghao = (GameObject)Instantiate(Resources.Load("Prefabs/" + "jinghao_right"));
            Jinghao.transform.parent = blockPos.transform;
            Jinghao.transform.localScale = new Vector3(1, 1, 1);
            Jinghao.transform.localPosition = body.transform.localPosition+new Vector3(132,37,0);
            //Debug.Log("进入");
            StartCoroutine(WaitAndPrint(1.0f));

            UIcamera.transform.GetComponent<DOTweenAnimation>().DOPlay();
            deadSource.Playdead_sound();
        }
        else if (collision.gameObject.CompareTag("road"))
        {
             Debug.Log("碰到了_物理碰撞");
            gameObject.transform.Find("role_run").GetComponent<Animator>().speed = 1;
        }
        
       
    }

    

    //public void OnCollisionEnter2D(Collision2D collision)
    //{
    //    Debug.Log("进来了");
    //    if (collision.gameObject.CompareTag("road"))
    //    {
    //        Debug.Log("碰到了_物理碰撞");
    //        gameObject.transform.Find("role_run").GetComponent<Animator>().speed = 1;
    //    }
    //}
    IEnumerator WaitAndPrint(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        panel_start.SetActive(true);
        panel_main.SetActive(false);
    }
}


2017-03-01 00:15:02 yi_2333 阅读数 309

Unity3D 2D贪吃蛇

Beat 1.0

只是实现了单个节点的移动,一个寒假没有用Unity 很多细节忘记了。反而在这里也花了很久的时间。

问题: 忘记将Rigibody 2D中的 Gravity Scale 设为0,导致在移动的时候一直往下掉,卡了蛮久想起来这个问题。

Beta 2.0

用队列得方式实现了贪吃蛇的移动,将身体依次入队,来实现贪吃蛇的链接,每次移动,只将最后一节身体出队,移到头节点刚刚离开的位置,然后入队。

因为每次移动都是走一格,控制速度是靠改变FixUpdate调用的频率

dest=transform.posiotion+dir*2

准备在3.0版本中试一下用一个节点跟着一个节点移动的方式实现一下。

素材包里没有墙的素材,还没做碰撞。大概会在3.0版本里补上

遇到的问题

原本吃食物用的碰撞检测是OnCollisionEnter2D 后来发现撞到食物后头结点的位置会稍微改变,导致后续身体节点的位置跟不上,会断开。所以将碰撞检测改成了 OnTriggerEnter2D


Beta 3.0

尝试了另一种运动方式,一个节点跟着一个节点得移动,并改变了移动方式所以看起来是更平滑的移动。

    Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
    GetComponent<Rigidbody2D>().MovePosition(p);
改进

增加了一个变量,判断何时改变蛇头的图片,这样就不会蛇头的图片已经换了方向,然而蛇还没转弯。

2.0的移动代码
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;


public class Move : MonoBehaviour {
    private Queue <GameObject> BodyQueue = new Queue<GameObject>();
    public GameObject body_1;
    public GameObject body_2;
    public GameObject body_3;
    public GameObject BodyPrefab;
    public Vector2 dir = Vector2.right;
    public Vector2 dest = Vector2.zero;
    public static bool New = false;
    const  float speed=2;
    // Use this for initialization
    void Start () {
        BodyQueue.Enqueue(body_3);
        BodyQueue.Enqueue(body_2);
        BodyQueue.Enqueue(body_1);
        dest = transform.position;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow) && dir != -Vector2.up)
             dir = Vector2.up;
        if (Input.GetKey(KeyCode.RightArrow) &&  dir != -Vector2.right)
             dir = Vector2.right;
        if (Input.GetKey(KeyCode.DownArrow) &&  dir != Vector2.up)
             dir = -Vector2.up;
        if (Input.GetKey(KeyCode.LeftArrow) &&  dir != Vector2.right)
             dir = -Vector2.right;
    }
    void FixedUpdate()
    {
        if ((Vector2)transform.position == dest)
        {
            HeadSprite.Changing = true;
            if (New)
            {
                BodyQueue.Enqueue(Instantiate(BodyPrefab, transform.position, Quaternion.identity));
                New = false;
            }
            else
            {
                GameObject last = BodyQueue.Dequeue();
                BodyQueue.Enqueue(last);
                last.transform.position = transform.position;
            }
            dest = (Vector2)transform.position + dir * speed;
        }
        transform.position = (Vector2)transform.position + dir * speed;
    }
}
3.0的蛇头移动代码
using UnityEngine;
using System.Collections;

public class HeadMove : MonoBehaviour
{
    public static float speed = 0.1f;
    public Vector2 dest;
    public Vector2 dir = Vector2.right;
    public Vector2 Lastdir = Vector2.right;
    // Use this for initialization
    void Start()
    {
        dest = transform.position;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if ((Vector2)transform.position == dest)
        {
            HeadSprite.Changing = true;
            Lastdir = dir;
            dest = (Vector2)transform.position + dir * 2;
        }
        Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
        GetComponent<Rigidbody2D>().MovePosition(p);

        if (Input.GetKey(KeyCode.UpArrow) && dir != -Vector2.up)
            dir = Vector2.up;
        if (Input.GetKey(KeyCode.RightArrow) && dir != -Vector2.right)
            dir = Vector2.right;
        if (Input.GetKey(KeyCode.DownArrow) && dir != Vector2.up)
            dir = -Vector2.up;
        if (Input.GetKey(KeyCode.LeftArrow) && dir != Vector2.right)
            dir = -Vector2.right;
    }
}
身体移动
 using UnityEngine;
using System.Collections;

public class BodyMove : MonoBehaviour {
    public static bool New;
    public static GameObject LastBody;
    public GameObject pre;
    public Vector2 dir=Vector2 .zero;
    public Vector2 Lastdir=Vector2 .zero ;
    public Vector2 dest;
    public float speed;
    public bool First;
    public bool Last;
    GameObject BodyPrefab;

    // Use this for initialization
    void Start () {
        New = false;
        speed = HeadMove.speed;
        dest = transform.position;
        BodyPrefab = (GameObject )Resources.Load("Prefabs/Body");
        if(!Last)
            LastBody = GameObject.Find("Body (2)");
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if ((Vector2)transform.position == dest)
        {
            Lastdir = dir;
            if (First)
                dir = GameObject.Find("Head").GetComponent<HeadMove>().Lastdir;
            else
                dir = pre.GetComponent<BodyMove>().Lastdir;
            if (New&&Last)
                CreatBody();
            dest = (Vector2)transform.position + dir*2;
        }
        Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
        GetComponent<Rigidbody2D>().MovePosition(p);
    }
    void CreatBody()
    {
        GameObject NewBody;
        Vector3 InitialPosition = transform.position - (Vector3)Lastdir * 2;
        NewBody = Instantiate(BodyPrefab, InitialPosition, Quaternion.identity);
        NewBody.GetComponent<BodyMove>().Last  = true;
        NewBody.GetComponent<BodyMove>().First = false;
        NewBody.GetComponent<BodyMove>().pre = LastBody;
        Last = false;
        LastBody = NewBody;
        New = false;
    }
}
链接:http://pan.baidu.com/s/1slz0twL 密码:0qcq


2017-07-09 22:58:07 BeUniqueToYou 阅读数 505

(1).首先,准备一张制作2D帧动画的精灵序列集合,并用Unity编辑器将其切割为多张单独的Sprite对象。
这里写图片描述

(2).切割好后,把它们全部选中,按住Shift键单击第一张Sprite对象和最后一张Sprite对象,即可快速全部选中。等到 Preview 视图中加载完毕所有Sprite对象后,进行下一步。
这里写图片描述

(3).接着,将全部选中的Sprite对象拖拽至Hierarchy视图,加入到场景中,此时将弹出动画文件保存框,点击保存。
这里写图片描述

(4).保存之后,在Project视图中多了两个文件,一个是动画文件(Animation),一个是动画控制器文件(Animator)。在Hierarchy视图中多了一个Sprite动画游戏对象,它拥有Transform组件、Sprite Renderer组件和Animator组件。
这里写图片描述

(5).最后,点击运行游戏,即可在Game视图中看见循环播放的2D帧动画,这里为大雁煽动翅膀的飞翔动画。而帧动画播放的默认帧顺序,即为最初的精灵序列集合切割之后,多张单独的Sprite对象的排列顺序。
这里写图片描述

2012-11-23 01:10:30 love_hot_girl 阅读数 409

正交。正交size是屏幕高度象素一半。创建Cube并贴图,不要用面片,面片要旋转,并且无法做到1比1(乃10plane单位比1象素)


这种情况最大的问题是象素与真实的Unity1单位1米分离了,无法协作。

Unity3D 之2D动画机

阅读数 28

没有更多推荐了,返回首页