滚动的小球
1.导入素材包(若素材包不可用可以自己造出需要的小球和方块)并新建需要的平面等物体。
2.编写脚本,有关摄像机的脚本要能移动、发射小球,小球发射后有存在时间,还要制造方块堆,让它处在平面上。
3.最后运行一下检查是否有错误存在。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeMove : MonoBehaviour {
public float moveSpeed = 0.5f;
private float h, v;
private Vector3 direction;
// Use this for initialization
void Start () {
h = Time.deltaTime * (-moveSpeed);
v = 0;
direction = new Vector3(h, 0, v);
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
Vector3 direction = new Vector3(h, 0, v);
this.GetComponent<Transform>().Translate(direction);
/*if (Input.GetMouseButtonDown(0))
{
h = Time.deltaTime * (-moveSpeed);
}
else if (Input.GetMouseButtonDown(1))
{
h = Time.deltaTime * moveSpeed;
}
v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
h = -moveSpeed;
v = 0;
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
h = moveSpeed;
v = 0;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
h = 0;
v = moveSpeed;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
h = 0;
v = -moveSpeed;
}
else
{
v = 0;
h = 0;
}*/
direction = new Vector3(h, 0, v);
this.GetComponent<Transform>().Translate(direction);
}
}