• Unity 2D 效应器与来回移动的实现


    1.效应器

    Point Effector 2D: 点效应器。进入区域,吸引或排斥物体

    Area Effector 2D: 区域效应器,可以用来做马里奥的管道移动效果

    Surface Effector 2D :表面效应器。实现传送带效果

    PlatForm Effector 2D:平台效应器。实现2D游戏里面的台阶效果

    2.控制移动

    // Update is called once per frame
        void Update () {
            if (Input.GetKey(KeyCode.A))
            {
                MoveLeft();
            }
            if (Input.GetKey(KeyCode.D))
            {
                MoveRight();
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Jump();
            }
        }
            void MoveLeft()
        {
            var v = rgbd.velocity;
            v.x = -movespeed;
            rgbd.velocity = v;
            //gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.left * movespeed, ForceMode2D.Force);
        }
    
        void MoveRight()
        {
            var v = rgbd.velocity;
            v.x = movespeed;
            rgbd.velocity = v;
            //gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * movespeed, ForceMode2D.Force);
        }
    
        public void Jump()
        {
            if (isground)
            {
                gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpspeed, ForceMode2D.Impulse);
                isground = false;
            }
            
        }

    3.敌人来回移动

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class laihui : MonoBehaviour {
    
        public float movespeed=3.0f;
        private Vector2 currentposition;
        public float distnation=10.0f;
        private int face = 1;
    
        // Use this for initialization
        void Start () {
            currentposition = gameObject.transform.position;
        }
        
        // Update is called once per frame
        void Update () {
            if (face == 1)
            {
                gameObject.transform.Translate(Vector2.right * movespeed * Time.deltaTime);
            }
            if (face == 0)
            {
                gameObject.transform.Translate(Vector2.left * movespeed * Time.deltaTime);
            }
            if (gameObject.transform.position.x > currentposition.x+distnation)
            {
                face = 0;
            }
            if (gameObject.transform.position.x < currentposition.x)
            {
                face = 1;
            }
        }
    }
  • 相关阅读:
    C#编写最小花时隐藏为任务栏图标的Window appllication
    XML与HTML的结合
    敏捷思维-架构设计中的方法学(1)从方法论看架构设计
    敏捷思维-架构设计中的方法学(4)团队设计
    敏捷思维-架构设计中的方法学(2)架构设计的敏捷视图
    敏捷思维-架构设计中的方法学(5)简单设计
    hdu 1116(并查集+欧拉路判断)
    hdu 2145(最短路+排序)
    hdu 2377
    hdu 2962(最短路+二分)
  • 原文地址:https://www.cnblogs.com/ComputerPlayerJs/p/10511784.html
Copyright © 2020-2023  润新知