unity基础-坦克炮管旋转发射炮弹(向量基础,射线碰撞,物体实例化)

简介: unity基础-坦克炮管旋转发射炮弹(向量基础,射线碰撞,物体实例化)

👨‍💻个人主页:@元宇宙-秩沅

hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

本文由 秩沅 原创

收录于专栏 unity实战入门

你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!
栓Q

目录

案例视频如下:
unity-自动炮台(向量基础,射线碰撞,物体实例化)_哔哩哔哩_bilibili

1.场景模拟:

2.实现炮口自动旋转(鼠标点哪它就走哪)
1.确定悬旋转对象,旋转对象它的旋转轴是什么。

2.有涉及到一个射线碰撞的原理:发射光线记录碰撞信息。

3.涉及向量点乘,叉乘(原因是为了求出实时的偏移角的值)

4.脚本代码为:

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

public class ROtate : MonoBehaviour
{
// Start is called before the first frame update
Vector3 offset;//向量差
float angle; //偏移角度
Vector3 faxian;//法线向量
float rightOrLeft;//判断转向
float flag;
float speed = 50.0f;
void Start()
{

}

// Update is called once per frame
void Update()
{
    if(Input.GetMouseButton(0)) //长按鼠标一直运行
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
           //发射了一个与鼠标所点击的点一个位置将要与其发声碰撞(发射光线)
        RaycastHit xinxi; //定义一个保存位置信息的参数
        if(Physics .Raycast (ray ,out xinxi)) //返回两个值,一个给xinxi ,另一个返回的是bool的值也就true或者false
        {
            offset = xinxi.transform.position - transform.position;
            //记录实时的向量偏移差
            angle =Mathf.Rad2Deg *Mathf.Acos(Vector3.Dot(transform .forward .normalized ,offset.normalized  ));
            //Matf.Acos 返回的是弧度值 ,Mathf,cos返回的是余弦值
            //Mathf.Rad2Deg 弧度转化成角度
            faxian = Vector3.Cross(transform.forward, offset);
            //确定了法线
            rightOrLeft = Vector3.Dot(faxian,Vector3 .up );
            //让法线和世界坐标的up轴(上方)进行点乘,目的是判断她两的位置关系,
            if(rightOrLeft >=0)
            {
                flag = 1.0f;
            }
            else
            {
                flag = -1.0f;
            }
            if (angle != 0)

            { 
                transform.Rotate(Vector3.up *flag *Time.deltaTime *speed,Space.World );
            }


        }


    }
}

}

3.将炮弹发射出去(鼠标点哪 ,哪里发射,shoot脚
案例视频地址如下:

unity-自动炮塔之炮弹发射(向量基础,射线碰撞,实例化物体)_哔哩哔哩_bilibili

1.生成炮弹预制体,实时实例化该预制体

2.将炮弹预制体发射出去(赋予一个向前的力,速度)

3.脚本:

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

public class Shoot : MonoBehaviour
{
// Start is called before the first frame update

public GameObject paodan;//实例化的对象
public float speed = 5000.0f;//speed 炮弹发射速度
void Start()
{

}

// Update is called once per frame
void Update()
{
    if(Input .GetMouseButtonDown (1))//单击鼠标左键按下的那一刻执行
    {
        GameObject testpandan = GameObject.Instantiate(paodan, transform.position, transform.rotation);
        //实例化炮弹的信息
        Rigidbody BB = testpandan.GetComponent<Rigidbody>();
        //得到实例化之后刚体信息
        BB.AddForce(transform .forward  * speed );
    }
}

}

子弹发射的实例:
想实现子弹发射,那么首先得多次实例化物体

接下来小编以照相机作子弹得载体,实时发射子弹,实现类似打砖块得案例

1.构建场景:
添加由预制体构成得墙,然后再在Project面板母体中添加刚体组件,使得全部的预制体都有重力效果

然后添加一个我们要实例化的对象这时我们添加一个black的material的球phere,作为将要实例化的物体

2.添加脚本
下一步我们就以相加为发射器添加脚本了

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

public class shoot2 : MonoBehaviour
{
public GameObject phere; //定义一个游戏物体对象
// Start is called before the first frame update
public float speed;
void Start()
{

}

// Update is called once per frame
void Update()
{
    if(Input .GetMouseButtonDown(0))//当单击鼠标左键时实现
    {
        GameObject timeobjects = GameObject.Instantiate(phere, transform.position, transform.rotation);
        //此时每次实例化物体,phere ,得到它的位置信息,角度信息
        Rigidbody timeobjectson = phere.GetComponent<Rigidbody>();
        //实时定义一个刚体对象timeobject让他得到游戏物体的刚体组件
        timeobjectson.velocity = Vector3.forward * speed;
        //引用内置速度方法                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    }
}

}

3.控制发射器移动
那么这个时候的发射器是没有刚体组件的,那么怎么来实现移动呢看下面的脚本:

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

public class shoot : MonoBehaviour
{

// public Rigidbody camare;
public float speed =0 ;
// Start is called before the first frame update
void Start()
{
// camare = GetComponent ();

}

// Update is called once per frame
void Update()
{
    float a, b;
    a = Input.GetAxis("Horizontal");
    b = Input.GetAxis("Vertical");
    transform.Translate(new Vector3(a, b, 0) * Time.deltaTime * speed);
    //因为Update每秒50贞,Time.daltatime为50分之一,所以按照了每秒一米的速度移动

}

}
以上就是发射子弹的原理啦,喜欢的话点个赞同再走吧~
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!
栓Q

目录
相关文章
|
6月前
|
图形学
Unity射线检测的避坑指南
Unity射线检测的避坑指南
|
7月前
|
图形学
Unity遍历子物体以及孙物体
遍历子物体以及孙物体
|
4月前
|
C# 图形学
【Unity 3D】元宇宙案例之虚拟地球信息射线实战(附源码、演示视频和步骤 超详细)
【Unity 3D】元宇宙案例之虚拟地球信息射线实战(附源码、演示视频和步骤 超详细)
50 0
|
4月前
|
图形学
Unity 向量
Unity 向量
|
4月前
|
数据可视化 图形学
Unity射线编程详解
Unity射线编程详解
|
4月前
|
图形学
Unity用脚本创建物体
Unity用脚本创建物体
|
4月前
|
编译器 图形学
Unity用脚本获取物体和组件(下)
Unity用脚本获取物体和组件(下)
100 0
|
4月前
|
图形学
Unity 用脚本获取物体和组件(上)
Unity 用脚本获取物体和组件(上)
115 0
|
10月前
【Unity3D--自由观察模型】模型自动旋转+触屏旋转和缩放
展示3D模型,同时实现模型自动旋转和触屏旋转和缩放
228 0
|
10月前
|
API 图形学
【unity细节】unity中实例化的物体在Game视图无法显示
【unity细节】unity中实例化的物体在Game视图无法显示
258 0