Running Ball
控制小球在Z方向的运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed;
public float turnSpeed=10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float x=Input.GetAxis("Horizontal")
// transform.Translate(0,0,speed*Time.deltaTime);//控制小球在Z方向的运动
transform.Translate(x*turnSpeed*Time.deltaTime,0,speed*Time.deltaTime);
}
}
为了使得摄像机跟随小球运动,把摄像机拖进Player对象,使得摄像机成为小球的子物体,让小球运动带动摄像机。
然后将障碍物设置为预制体,给小球添加刚体组件,选中player 的rigidbody的Is kinematic,选中Barrier的BoxCollider 的Is Trigger,并添加脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Barrier : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.name+"碰到了我");
Time.timeScale=0;
}
}
为表示小球越过了平台道路,就下落,在Player脚本中添加如下
void Update()
{
if(Input.GetKeyDown(KeyCode.R)