前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Roll A Ball

Roll A Ball

作者头像
星河造梦坊官方
发布2024-08-14 15:42:24
830
发布2024-08-14 15:42:24
举报
文章被收录于专栏:星河造梦坊专栏

//FollowPlayer(相机跟随主角)、food(控制food旋转)、Player(控制主角移动、碰撞器)

U3D源文件网盘链接

FollowPlayer,相机跟随主角

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    public GameObject player;

    // Update is called once per frame
    void Update()
    {
        Vector3 player_position = player.GetComponent<Transform>().position;
        this.GetComponent<Transform>().position = new Vector3(player_position.x, player_position.y + 13.14f, player_position.z - 13.98f);
    }
}

控制food旋转

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class food : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(Vector3.up);                        //此处transform小写!
    }
}

Player,控制主角移动、碰撞器-is Trigger触发器

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    int i = 0;
    // Update is called once per frame
    void Update()
    {
        float horizontal_move = Input.GetAxis("Horizontal");   //horizontal必须与unity名称一模一样!
        float vertical_move = Input.GetAxis("Vertical");  //unity位置:Edit-Projectings-Input
        this.GetComponent<Rigidbody>().AddForce(new Vector3(horizontal_move, 0, vertical_move) * 10);
    }

    void OnTriggerEnter(Collider other)     //OnTriggerEnter是一个方法,且i不能定义在它里面,每次调用这个方法,i都会被初始化!
    {
        if (other.gameObject.name == "food")
        {                                         //i不能定义在这里面!因为if每次都会将i初始化为0,永远都不可能“win”!
            GameObject.Destroy(other.gameObject);
            i++;
            if (i == 10)
            {
                Debug.Log("U are win ! ");
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • FollowPlayer,相机跟随主角
  • 控制food旋转
  • Player,控制主角移动、碰撞器-is Trigger触发器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档