Super ball

初学unity,老师要求上传的一个小游戏。
首先构建出一个场景,类似于以前玩的潜水艇过障碍的模型,使小球通过这些障碍获胜;如图所示:在这里插入图片描述
下面是代码展示(以下代码均为C#)
这是自动摧毁屏障(障碍物)的代码:

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

public class AutoDestoryBarrier : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);
    }
}

这是控制障碍物的代码:

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

public class BarrierControl : MonoBehaviour {
    public GameObject player;
    public GameObject BarrierPre;
    public GameObject CurrentBarrier;
    public int BarrierInterval = 10;
	
	void Start () {
		
	}
	
	void Update () {
        AutCreatBarrier();

    }
    public void AutCreatBarrier()
    {
        if(player.transform.position.x>CurrentBarrier.transform.position.x)
        {
            //生成新的障碍物
            float targetX = CurrentBarrier.transform.position.x+BarrierInterval;
            float targetY = RandomBarrierPositionY();
            Vector3 targetPos = new Vector3(targetX,targetY,0);
            GameObject g = Instantiate(BarrierPre,targetPos,Quaternion.identity);
            g.transform.localScale = new Vector3(g.transform.localScale.x, RandomBarrierSizeY((int)g.transform.position.y),g.transform.localScale.z);
            CurrentBarrier = g;
        }
    }
    public float RandomBarrierSizeY(int y)
    {
        int yAbs = Mathf.Abs(y);
        if(yAbs==0)
        {
            return 6;
        }
        else
        {
            return (3 - yAbs) * 2 - 1;
        }

    }
    public float RandomBarrierPositionY()
    {
        int r = Random.Range(-3,3);
        return r;
    }
}

这是控制相机移动的代码:

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

public class cameraControl : MonoBehaviour {
    public GameObject player;
    private float offset_camera;
	void Start () {
        offset_camera = gameObject.transform.position.x - player.transform.position.x;
	}
	
	// Update is called once per frame
	void Update () {
        FollowCameraMove();

    }
    void FollowCameraMove()
    {
        gameObject.transform.position = new Vector3(offset_camera + player.transform.position.x, gameObject.transform.position.y,gameObject.transform.position.z);
    }
}

这是控制小球移动(通过w、s、a、d控制小球移动)的代码:

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

public class playMove : MonoBehaviour {
    public Rigidbody rd;
    public float speedAutoMove = 5;
    public float speedMoveUpandDown = 20;
	void Start () {
        rd = gameObject.GetComponent<Rigidbody>();
	}
	
	void Update () {
        PlayerAutoMove();
        PlayerMoveUpandDown();

    }
    private void PlayerAutoMove()
    {
        rd.AddForce(Vector3.right*speedAutoMove);//前进
    }
    private void PlayerMoveUpandDown()
    {
        float v = Input.GetAxis("Vertical");//上下运动
        rd.AddForce(v*Vector3.up*speedMoveUpandDown);//给一个上下的力量
    }
}

这是控制墙的代码:

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

public class wallControl : MonoBehaviour {
    private float offset;
    public GameObject player;
	// Use this for initialization
	void Start () {
        offset = gameObject.transform.position.x - player.transform.position.x;
	}
	
	// Update is called once per frame
	void Update () {
        FollowPlayerMove();
	}
    void FollowPlayerMove()
    {
        gameObject.transform.position = new Vector3(player.transform.position.x+offset,0,0);
    }
}

运动效果:
运动效果

不喜勿喷,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你如镜难忘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值