Roll-a-Ball教程学习笔记(未整理)

本文记录了学习Unity中的Roll-a-Ball教程的过程,详细分析了PlayController.cs和CameraController.cs两个关键组件的源码,探讨了游戏逻辑和相机控制的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Roll-a-Ball教程学习笔记(未整理)


环境:Unity3D5 5.1.1f1
作者:kagula


简介
  资料资料[1]学习Unity3D基本概念,并做了笔记。


注意事项
[1]
FixedUpdate事件,响应物理动作,同帧率无关。


[2]
关键词(函数名)后面跟上Ctrl+',打开Help document.


[3]
关于空间
Vector3(LeftOrRight, UpOrDown, ForwardOrBackward)


[4]添加了“Rigidbody”component的游戏对象可以接受外力和扭矩力,
并且像真实的物体一样接受摩擦力和重力。


[5]LastUpdate事件,所有item运算完成后,就激发这个事件。


[6]prefab的概念就像个“原始模板”,对它的修改会影响它的所有实例。


[7]静态对象不要移动它,否则会导致刷新静态对象cache,性能会降低,
   所以会动的东西,要加上RigidBody component,这样就不是“静态对象”了,改进了效率.


[8]Collider的Is Trigger属性,是不是意思说,其它对象碰到它,会触发OnTriggerEnter事件?


教程中用到的源代码

PlayController.cs源码清单

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

public class PlayerController : MonoBehaviour {
	public float speed;
	public Text countText;
	public Text winText;

	private Rigidbody rb;
	private int count;

	void Start()
	{
		rb = GetComponent<Rigidbody> ();
		count = 0;
		countText.text = "Count:" + count.ToString ();
		winText.text = "";
		SetCountText();
	}

	void FixedUpdate()
	{
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		rb.AddForce (movement * speed);
	}

	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.CompareTag ("Pick Up")) {
			other.gameObject.SetActive(false);
			count = count + 1;
			SetCountText();
		}
	}

	void SetCountText()
	{
		countText.text = "Count:" + count.ToString ();
		if (count >= 10) {
			winText.text = "You Win";
		}
	}
}


CameraController.cs源码清单

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {
	public GameObject player;
	private Vector3 offset;
	// Use this for initialization
	void Start () {
		offset = transform.position - player.transform.position;	
	}
	
	// Update is called once per frame
	void Update () {
		transform.position = player.transform.position + offset;
	}
}


Rotator.cs源码清单

using UnityEngine;
using System.Collections;

public class Rotator : MonoBehaviour {
	// Update is called once per frame
	void Update () {
		transform.Rotate (new Vector3 (13, 30, 45) * Time.deltaTime);
	}
}




参考资料
[1]Roll-a-Ball教程地址
https://unity3d.com/learn/tutorials/projects/roll-a-ball/creating-collectables
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kagula086

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

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

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

打赏作者

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

抵扣说明:

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

余额充值