小球快跑
1.组建场景(镜头对准正方向如1-2)
1-1
1-2
2.通过键盘控制小球移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playMove : MonoBehaviour {
public Rigidbody rd;
public float speedAutoMove = 5;
public float speedMoveUpandDown = 20;
void Start () {
rd = gameObject.GetComponent();
}
void Update () {
PlayerAutoMove();
PlayerMoveUpandDown();
}
private void PlayerAutoMove()
{
rd.AddForce(Vector3.right*speedAutoMove);//向前
}
private void PlayerMoveUpandDown()
{
float v = Input.GetAxis("Vertical");//上下运动
rd.AddForce(v*Vector3.up*speedMoveUpandDown);//给一个上下的F
}
}
3.相机跟随小球跑动以免小球跑出视角。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraControl : MonoBehaviour {
public GameObject player;
private float offset_camera;
void Start () {
offset_camera = gameObject.transform.position.x - player.transform.position.x;
}
// Update is called once per frame
void Update () {
FollowCameraMove();
}
void FollowCameraMove()
{
gameObject.transform.position = new Vector3(offset_camera + player.transform.position.x, gameObject.transform.position.y,gameObject.transform.position.z);
}
}
4.上下边框跟随小球移动避免小球掉落(上下边界的)
首先可以将上下边框的z方向宽度调宽一些防止小球掉落
5.