滚球案例学习笔记

所有脚本

相机跟随

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

public class FollowTarget : MonoBehaviour
{
    public Transform PlayerTransform;
    private Vector3 offset;//因为不需要通过外界设置值,所以用private
    // Start is called before the first frame update
    void Start()
    {
        //先定义两个位置的差                       引用Player上的组件,要先在最开始定义
        offset = transform.position - PlayerTransform.position;
        //定义用Vector3 offset ,但是在这个方法里定义的值无法被其他方法引用
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = PlayerTransform.position+offset;//不能直接引用到offsrt,因此把offset转化成属性
    }
}

物品旋转

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

public class Food : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //旋转的实现方法,用Rotate,设置旋转角度为上
        transform.Rotate(Vector3.up);
        
    }

    
}

玩家操作

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

public class Player : MonoBehaviour
{
    public GameObject Wintext;
    public Text scoreText;
    public int score = 0;//定义一个分数,整数变量
    public Rigidbody rd;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log("游戏开始了");
        rd = GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void Update()
    {
        //rd.AddForce(new Vector3(10, 0, 0));

        float h = Input.GetAxis("Horizontal");//-1 1 ad
        float v = Input.GetAxis("Vertical");//ws
        //Debug.Log(h);
        rd.AddForce(new Vector3(h, 0, v));

    }
    //使用一个系统事件写碰撞方法 碰撞有三个状态 这里只需要用到enter collision保存了碰撞信息
    private void OnCollisionEnter(Collision collision)
    {
        //collision.collider.tag;无论是通过游戏物体还是通过碰撞组件,都可以获得物体的标签

        if (collision.gameObject.tag == "Food")//判断碰撞体是否是Food
        {
            Destroy(collision.gameObject);
        }
        //Debug.Log("发生了碰撞 OnCollisionEnter");
    }
    //private void OnCollisionExit(Collision collision)
    //{
    //    //Debug.Log("发生了碰撞 OnCollisionExit");

    //}
    //private void OnCollisionStay(Collision collision)
    //{
    //    //Debug.Log("发生了碰撞 OnCollisionStay");

    //}
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Food")//判断碰撞体是否是Food
        {
            Destroy(other.gameObject);
            score++;
            scoreText.text = "分数:" + score;

            if (score == 8)
            {
                Wintext.SetActive(true);
            }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值