Game Parkour by 764424567 - 7

Games & Projects

Unity开发跑酷游戏

Unity 5.6.1f1MIT LicenseUpdated 24 days agoCreated on January 10th, 2020
Go to source

# Game_Parkour

Unity开发跑酷游戏

## 三、教程

### 六、生成障碍物

``````using UnityEngine;

public class Control_Scenes : MonoBehaviour
{
public GameObject[] m_ObstacleArray;
public Transform[] m_ObstaclePosArray;

void Start()
{
Spawn_Obstacle(1);
}

//生成障碍物
public void Spawn_Obstacle(int index)
{
//销毁原来的对象
GameObject[] obsPast = GameObject.FindGameObjectsWithTag("Obstacle" + index);
for (int i = 0; i < obsPast.Length; i++)
{
Destroy(obsPast[i]);
}
//生成障碍物
foreach (Transform item in m_ObstaclePosArray[index])
{
GameObject prefab = m_ObstacleArray[Random.Range(0, m_ObstacleArray.Length)];
Vector3 eulerAngle = new Vector3(0, Random.Range(0, 360), 0);
GameObject obj = Instantiate(prefab, item.position, Quaternion.Euler(eulerAngle));
obj.tag = "Obstacle" + index;
}
}
}

``````

### 七、路段切换

``````void Start()
{
//Spawn_Obstacle(1);
}
{
if (m_ISFirst && index == 0)
{
m_ISFirst = false;
return;
}
else
{
int lastIndex = index - 1;
if (lastIndex < 0)
lastIndex = 2;
Spawn_Obstacle(lastIndex);
}
}
``````

PS：这里解释一下代码，怎么切换的呢 举个例子，角色跑到了第二段，那么第一段要移动到第三段后面隔一个路段长度的距离，接下来画个图： 那么为啥x轴减去150。这是因为我发现这三条路段的距离都差了50，坐标轴是负轴，所以就减去了150。

## 八、角色移动

``````using UnityEngine;

public class Control_Player : MonoBehaviour
{
//前进速度
public float m_ForwardSpeeed = 7.0f;
//动画组件
private Animator m_Anim;
//动画现在状态
private AnimatorStateInfo m_CurrentBaseState;

//动画状态参照
static int m_jumpState = Animator.StringToHash("Base Layer.jump");
static int m_slideState = Animator.StringToHash("Base Layer.slide");

// Use this for initialization
void Start()
{
m_Anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
transform.position += Vector3.left * m_ForwardSpeeed * Time.deltaTime;
m_CurrentBaseState = m_Anim.GetCurrentAnimatorStateInfo(0);
if (Input.GetKeyDown(KeyCode.W))
{
m_Anim.SetBool("jump", true);
}
else if (Input.GetKeyDown(KeyCode.S))
{
m_Anim.SetBool("slide", true);
}
else if (Input.GetKeyDown(KeyCode.A))
{
Change_PlayerZ(true);
}
else if (Input.GetKeyDown(KeyCode.D))
{
Change_PlayerZ(false);
}
if (m_CurrentBaseState.fullPathHash == m_jumpState)
{
m_Anim.SetBool("jump", false);
}
else if (m_CurrentBaseState.fullPathHash == m_slideState)
{
m_Anim.SetBool("slide", false);
}
}

{
{
if (transform.position.z == -14.7f)
return;
else if (transform.position.z == -9.69f)
{
transform.position = new Vector3(transform.position.x, transform.position.y, -14.7f);
}
else
{
transform.position = new Vector3(transform.position.x, transform.position.y, -9.69f);
}
}
else
{
if (transform.position.z == -6.2f)
return;
else if (transform.position.z == -9.69f)
{
transform.position = new Vector3(transform.position.x, transform.position.y, -6.2f);
}
else
{
transform.position = new Vector3(transform.position.x, transform.position.y, -9.69f);
}

}
}
}

``````

### 九、摄像机跟随

``````using UnityEngine;

public class Control_Camera : MonoBehaviour
{
//间隔距离
public float m_DistanceAway = 5f;
//间隔高度
public float m_DistanceHeight = 10f;
//平滑值
public float smooth = 2f;
//目标点
private Vector3 m_TargetPosition;
//参照点
Transform m_Follow;

void Start()
{
}

void LateUpdate()
{
m_TargetPosition = m_Follow.position + Vector3.up * m_DistanceHeight - m_Follow.forward * m_DistanceAway;
transform.position = Vector3.Lerp(transform.position, m_TargetPosition, Time.deltaTime * smooth);
}
}

``````

OK 摄像机跟着Player跑起来了

### 十、碰撞检测

``````//游戏结束
bool m_IsEnd = false;
void OnGUI()
{
if (m_IsEnd)
{
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.MiddleCenter;
style.fontSize = 40;
style.normal.textColor = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100), "你输了~", style);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Vehicle_DumpTruck" || other.gameObject.name == "Vehicle_MixerTruck")
{
m_IsEnd = true;
m_ForwardSpeeed = 0;
m_Anim.SetBool("idle", true);
}
}
``````

### 十一、切换道路

`````` //场景控制对象
Control_Scenes m_ControlScenes;
void Start()
{
m_Anim = GetComponent<Animator>();
m_ControlScenes = GameObject.Find("Control_Scenes").GetComponent<Control_Scenes>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Vehicle_DumpTruck" || other.gameObject.name == "Vehicle_MixerTruck")
{
m_IsEnd = true;
m_ForwardSpeeed = 0;
m_Anim.SetBool("idle", true);
}
if (other.gameObject.name == "MonitorPos0")
{