游戏机制:
射箭是一款益智游戏,玩家需要在界面中点击右键来进行视角移动,分别点击W、A、S和D来进行人物前后左右的移动,玩家可在预设的两个射击点长按左键进行蓄力,松开左键进行射击,箭在射中靶子后就能对靶子造成伤害,伤害达到一定数值后就能得分。
制作代码:
ArrowCtrl.cs
实现对箭矢的控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowCtrl : MonoBehaviour
{
public Rigidbody self_rigidbody;
public float force=1000;
public Vector3 last_position;
public bool hit=true;
public Collider arrow_hit;
public TrailRenderer trail;
public float hit_time;
public bool trail_destroy=false;
public void OnCollisionEnter(Collision collision) {
foreach(ContactPoint f in collision.contacts){
if(self_rigidbody.constraints != RigidbodyConstraints.FreezeAll){
// print("error 1");
// foreach(ContactPoint f in collision.contacts){
if(f.thisCollider==arrow_hit){
// print("error 2");
if(!collision.gameObject.GetComponent<ArrowCtrl>()){
transform.parent=collision.transform;
self_rigidbody.constraints=RigidbodyConstraints.FreezeAll;
hit=false;
// print("error 3");
if(collision.gameObject.GetComponentInParent<TargetCtrlOne>()){
collision.gameObject.GetComponentInParent<TargetCtrlOne>().hurt(force*.08f);
// print("error 4");
}
else if(collision.gameObject.GetComponentInParent<TargetCtrlTwo>()){
collision.gameObject.GetComponentInParent<TargetCtrlTwo>().hurt(force*.08f);
// print("error 5");
}
}
}
}
}
}
// Start is called before the first frame update
void Start()
{
self_rigidbody.AddForce(transform.forward*force);
}
// Update is called once per frame
void Update()
{
// if(hit_time==0)
if(!hit)
{
this.hit_time += (Time.deltaTime);
// print(hit_time);
if(hit_time>=1){
this.trail.enabled = false;
// print("error 5");
}
}
else{
if(transform.position!=last_position){
transform.forward=transform.position-last_position;
}
last_position=transform.position;
}
}
}
CenterCtrl.cs
实现控制核心
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CenterCtrl : MonoBehaviour
{
public ArrowCtrl bow_template;
public Transform transmit_point;
public Transform arrow_point;
public Transform bow_model;
public Transform bow_platform;
public float model_speed=.00001f;
public float platform_speed=.00001f;
public float strength;
public AnimationCurve force_curve;
public Vector3 last_mouse_position;
// public TargetCtrl target_template;
// public float generate_time=3;
// public float generate_timekeeping=0;
// public Transform[] generate_position;
// public Transform[] generate_move_position;
public Transform min_force_point;
public Transform max_force_point;
public GameObject art;
public LineRenderer rendering;
public Transform point1,point3;
public float move_speed=5;
public int arrow_count=0;
public bool shooting=false;
public UnityEngine.UI.Text trend_score;
public UnityEngine.UI.Text static_score;
public UnityEngine.UI.Text static_counter;
public UnityEngine.UI.Text skybox_change;
public PointOne potone;
public PointTwo pottwo;
public PointThree potthree;
public Vector3 temp_rotation;
public Vector3 curr_angel;
public Vector3 max_angel;
public Vector3 min_angel;
public TargetCtrlOne[] tco;
public TargetCtrlTwo[] tct;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// generate_timekeeping-=Time.deltaTime;
if(potone.shoot){
static_counter.text="";
static_score.text="Trend Score:"+potone.score;
}
if(!potone.shoot){
static_score.text="";
}
if(pottwo.shoot){
trend_score.text="Static Score:"+pottwo.score;
static_counter.text="";
}
if(!pottwo.shoot){
trend_score.text="";
}
if(potthree.isinarea){
skybox_change.text="Skybox changes!";
}
if(!potthree.isinarea){
skybox_change.text="Skybox not change";
}
if(!pottwo.shoot&&!potone.shoot){
static_counter.text="";
}
// if(potthree.shoot){
// }
// if(generate_timekeeping<=0){
// generate_timekeeping=generate_time;
// Transform temp_generate_position=generate_position[Random.Range(0,generate_position.Length)];
// Instantiate(target_template,temp_generate_position.position,temp_generate_position.rotation).move_target
// =generate_move_position[Random.Range(0,generate_move_position.Length)].position;
// }
if(Input.GetKeyDown(KeyCode.Mouse0)&&arrow_count>0){
// print("hello");
strength=0;
arrow_count--;
shooting=true;
}
if(Input.GetKey(KeyCode.Mouse0)&&shooting){
strength+=Time.deltaTime;
}
arrow_point.position=Vector3.Lerp(min_force_point.position,max_force_point.position,strength/force_curve.keys[force_curve.length-1].time*10);
art.SetActive(Input.GetKey(KeyCode.Mouse0));
rendering.SetPosition(0,point1.position);
rendering.SetPosition(1,arrow_point.position);
rendering.SetPosition(2,point3.position);
if(Input.GetKeyUp(KeyCode.Mouse0)&&shooting){
ArrowCtrl temp_object=Instantiate(bow_template,transmit_point.position,transmit_point.rotation);
temp_object.force=force_curve.Evaluate(strength);
strength=0;
shooting=false;
}
if(Input.GetKeyDown(KeyCode.Mouse1)){
last_mouse_position=Input.mousePosition;
}
if(Input.GetKey(KeyCode.Mouse1)){
bow_platform.Rotate(Vector3.up*(last_mouse_position-Input.mousePosition).x*platform_speed);
// bow_model.Rotate(Vector3.right*(last_mouse_position-Input.mousePosition).y*model_speed);
temp_rotation=Input.mousePosition-last_mouse_position;
temp_rotation=new Vector3(temp_rotation.x,temp_rotation.y,0)*model_speed;
curr_angel+=temp_rotation;
if(max_angel.y>=360||(curr_angel.y>=min_angel.y&&curr_angel.y<=max_angel.y))
{
bow_model.Rotate(Vector3.right*(last_mouse_position - Input.mousePosition).y*model_speed);
}
else
{
curr_angel -= Vector3.up*temp_rotation.y;
}
}
last_mouse_position=Input.mousePosition;
if(Input.GetKey(KeyCode.W)){
bow_platform.Translate(Vector3.forward*Time.deltaTime*move_speed);
}
if(Input.GetKey(KeyCode.A)){
bow_platform.Translate(Vector3.left*Time.deltaTime*move_speed);
}
if(Input.GetKey(KeyCode.S)){
bow_platform.Translate(Vector3.back*Time.deltaTime*move_speed);
}
if(Input.GetKey(KeyCode.D)){
bow_platform.Translate(Vector3.right*Time.deltaTime*move_speed);
}
}
}
PointOne.cs
一号射击点(动态)控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointOne : MonoBehaviour
{
public CenterCtrl center;
public int count;
public float score;
public bool shoot=false;
public void OnTriggerEnter(Collider collision)
{
count = 2;
if(!collision.gameObject.GetComponent<ArrowCtrl>()){
ShootStart();
}
}
public void OnTriggerExit(Collider collision)
{
if(!collision.gameObject.GetComponent<ArrowCtrl>()){
score = 0;
ShootStop();
}
}
public void ShootStart()
{
center.arrow_count = 10;
shoot = true;
}
public void ShootStop()
{
center.arrow_count = 0;
shoot = false;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
PointTwo.cs
二号射击点(静态)控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointTwo : MonoBehaviour
{
public CenterCtrl center;
public int count;
public float score;
public bool shoot=false;
public void OnTriggerEnter(Collider collision)
{
count = 3;
if(!collision.gameObject.GetComponent<ArrowCtrl>()){
ShootStart();
}
}
public void OnTriggerExit(Collider collision)
{
if(!collision.gameObject.GetComponent<ArrowCtrl>()){
score = 0;
ShootStop();
}
}
public void ShootStart()
{
center.arrow_count = 10;
shoot = true;
}
public void ShootStop()
{
center.arrow_count = 0;
shoot = false;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
PointThree.cs
三号点(天空盒切换点)控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointThree : MonoBehaviour
{
public SkyboxChange skyboxchanger;
public bool isinarea=false;
public void OnTriggerEnter()
{
if(isinarea==false){
skyboxchanger.change();
isinarea=true;
}
}
public void OnTriggerExit()
{
isinarea=false;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
SkyboxChange.cs
天空盒切换控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkyboxChange : MonoBehaviour
{
public Material[] skyboxlist;
// public Light sun;
public int index;
public bool daytime=true;
public void change()
{
RenderSettings.skybox = skyboxlist[index];
index++;
index %= skyboxlist.Length;
if(daytime)
{
daytime=false;
// sun.enabled = false;
}
else
{
daytime=true;
// sun.enabled = true;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
TargetCtrlOne.cs
动态靶子控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetCtrlOne : MonoBehaviour
{
public float max_health=100000;
public float curr_health=100000;
public Vector3 move_target;
public Animator animation_ctrl;
public PointOne potone;
public float score=3;
public bool isshoot=false;
public void getScore()
{
potone.score += score;
}
public void death(){
animation_ctrl.Play("static");
// animation_ctrl.Play("Exit");
// print( potone.score);
potone.score += 3;
potone.count--;
// getScore();
// print( potone.score);
}
public void hurt(float damage){
curr_health=Mathf.Clamp(curr_health-damage,0,max_health);
if(curr_health<=0&&!isshoot){
death();
isshoot=true;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
TargetCtrlTwo.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetCtrlTwo : MonoBehaviour
{
public float max_health=1;
public float curr_health=1;
public Vector3 move_target;
// public Animator animation_ctrl;
public PointTwo pottwo;
public float score=3;
public bool isshoot=false;
public void getScore()
{
pottwo.score += score;
}
public void death(){
// animation_ctrl.Play("static");
// print( pottwo.score);
pottwo.score += 3;
pottwo.count--;
// print(pottwo.score);
// print("hello");
}
public void hurt(float damage){
curr_health=Mathf.Clamp(curr_health-damage,0,max_health);
// print(curr_health);
if(curr_health<=0&&!isshoot){
death();
isshoot=true;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}