3D游戏编程blog6

改进飞碟游戏

游戏内容要求

1.按adapter模式设计图修改飞碟游戏
2.使他同时支持物理学和运动学(变换)运动

本次代码与上次几乎完全相同,重点是使用了adapter模式,新增adapter脚本,代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Adapter : SSActionManager {
    public DiskFlyAction fly;
    public PhysisFlyAction fly_;

    public void DiskFly(GameObject disk, int mode, mes information) {
        int leftOrRight = 1;//from left is 1, from right is -1
        if (disk.transform.position.x > 0){
            leftOrRight = -1;
        }

        if(mode == 2){
            fly = DiskFlyAction.GetSSAction(leftOrRight, information.angle, information.speed);
            this.StartAction(disk, fly);
        }
        else{
            fly_ = PhysisFlyAction.GetSSAction(leftOrRight, information.speed);
            this.StartAction(disk, fly_);
        }
    }
}

物理学飞行轨迹新增PhisisFlyAction,具体代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhysisFlyAction : SSAction
{
    private bool start_position;
    public float force;

    public static PhysisFlyAction GetSSAction(int pos, float force_){
        PhysisFlyAction action = CreateInstance<PhysisFlyAction>();
        if(pos == 1){
            action.start_position = true;
        }
        else{
            action.start_position = false;
        }
        action.force = force_;
        
        return action;
    }
    public override void Start()
    {
        Rigidbody disk = gameobject.GetComponent<Rigidbody>();
        
        gameobject.GetComponent<Rigidbody>().useGravity = true;
        if(gameobject.GetComponent<Rigidbody>().position.y <= 3){
            if(start_position){
                gameobject.GetComponent<Rigidbody>().AddForce(new Vector3(0.4f,0.2f,0)*force*15f, ForceMode.Impulse);
            }
            else{
                gameobject.GetComponent<Rigidbody>().AddForce(new Vector3(-0.4f,0.2f,0)*force*15f, ForceMode.Impulse);
            }
        }
    }

    public override void Update()
    {
        
    }

    public override void FixedUpdate() {
        if (transform.position.y <= -10f) {
            Debug.Log(transform.position.y);
            gameobject.GetComponent<Rigidbody>().useGravity = false;
            gameobject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
            this.enable = false;
        }
    }
}

初始ui发生了一定变化:

ui图

游戏效果与其余代码与上次实验几乎完全一致,Assets资源链接:
hw6 · 灬蔷薇绅士灬/3D - 码云 - 开源中国 (gitee.com)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容