Unity射击项目练手(添加基本场景与射击)

目录

一,创建场景

二.添加角色

三,添加子弹

四,添加敌人

五,运行效果


一,创建场景

        Unity射击项目练手(添加基本场景与射击)_第1张图片

 新建一个立方体拉伸,成地面.

再加上四面的墙,和场景内的阻碍物.

二.添加角色

Unity射击项目练手(添加基本场景与射击)_第2张图片

 创建一个圆柱体,充当角色.

然后给角色挂上物理组件

Unity射击项目练手(添加基本场景与射击)_第3张图片

移动摄像机到合适位置

Unity射击项目练手(添加基本场景与射击)_第4张图片 

给摄像机挂上跟随主角移动脚本

    public Transform player;

    private Vector3 pos;
    // Start is called before the first frame update
    void Start()
    {
        pos = player.position- transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = player.position - pos;
    }

 Unity射击项目练手(添加基本场景与射击)_第5张图片

 添加角色移动控制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    private Rigidbody rigidbody;

    public int Speed;
    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent();
    }
    // Update is called once per frame
    void LateUpdate()
    {
        if (rigidbody != null)
        {
            if (Input.GetKey(KeyCode.W)) {

                rigidbody.AddForce(Vector3.left*Speed);
           }
            if (Input.GetKey(KeyCode.D))
            {

                rigidbody.AddForce(Vector3.forward * Speed);
            }
            if (Input.GetKey(KeyCode.S))
            {

                rigidbody.AddForce(Vector3.right * Speed);
            }
            if (Input.GetKey(KeyCode.A))
            {

                rigidbody.AddForce(Vector3.back * Speed);
            }
        }
    }
}

运行一下,主角可以被键盘控制移动,摄像机跟随着移动.

三,添加子弹

        新建一个球形,缩放到合适大小,可以添加上红色的材质,然后拉到文件夹内,变为预制体.

设置枪口位置.

 

using JetBrains.Rider.Unity.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fire : MonoBehaviour
{
    public GameObject Buton;
    public Transform ButonPosition;

    public int BuSpeed;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GameObject go =  Instantiate(Buton);
            go.transform.SetParent(ButonPosition, false);
            go.transform.localPosition = Vector3.zero;
            go.GetComponent().AddForce((transform.right+transform.GetComponent().velocity/10)* BuSpeed );
            Destroy(go,3f);
        }
    }
}

鼠标左键发射,然后3秒之后自动销毁,把这个脚本挂在在主角上,然后引用都加上.

Unity射击项目练手(添加基本场景与射击)_第6张图片

运行起来,键盘控制移动,鼠标发射子弹. 

Unity射击项目练手(添加基本场景与射击)_第7张图片

四,添加敌人

把角色拉到文件夹,变为预制体,然后在拖到场景内,改个材质,先只加一个自动射击脚本.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIFair : MonoBehaviour
{
    public GameObject Buton;
    public Transform ButonPosition;
    private int cd;
    // Start is called before the first frame update
    void Start()
    {
        cd = 0;
    }

    // Update is called once per frame
    void Update()
    {
        cd++;
        if (cd >30) {
            cd= 0;
            GameObject go = Instantiate(Buton);
            go.transform.SetParent(ButonPosition, false);
            go.transform.localPosition = Vector3.zero;
            go.GetComponent().AddForce((transform.right + transform.GetComponent().velocity / 10) * 500);
            Destroy(go, 5f);
        }
    }
}

五,运行效果

Unity射击项目练手(添加基本场景与射击)_第8张图片

 

你可能感兴趣的:(Unity项目,unity,游戏引擎,游戏)