[Unity] Unity3D常见技术点(持续更新)

查看:488 |回复:0 | 2020-5-9 10:55:40

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 源支始 于 2020-5-21 17:42 编辑

一:获取对象, 添加对象等
1:使用prefab生成对象
GameObject ballObj = GameObject.Instantiate(Resources.Load("Fx/fx_bullet001"),
transform.position + transform.forward * -0.8f + transform.up * 2,
Quaternion.identity) as GameObject;

2:添加脚本到对象, 并更改脚本值
[size=1em]
1

2

3

4

5

6

     
ballObj.AddComponent ("BasicGun");

BasicGun pScript = ballObj.GetComponent ("BasicGun") as BasicGun;  
pScript.player = playerObj;
   




3:在UIButton对象中获取 UIButton自身.
[size=1em]
1

UISprite sprite = gameObject.GetComponentInChildren<UISprite> ();




二: 旋转相关
1:让一个对象与另一个对象的旋转角度一样(即朝向同一个方向)

[size=1em]
1

2

3

4

5

6

7

8

9

// 主角的朝向
Vector3 dVector = playerObj.transform.forward;

// 计算要旋转的角度
float testA = Mathf.Atan2(dVector.x, dVector.z);
testA = testA* Mathf.Rad2Deg;  //本函数将 number 从弧度转换为角度   rad2deg(M_PI_4); // 45

// 对象旋转到对应角度
ballObj.transform.rotation = Quaternion.Euler(new Vector3(0, testA,0));



  ps: 不能直接为transform.rotation赋值。可以使用各种Quaternion的方法。

2: 旋转某对象的 方向
[size=1em]
1

ballObj.transform.Rotate(Vector3.up, 30);




[size=1em]
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Update() {
                // Slowly rotate the object around its X axis at 1 degree/second.
           //围绕x轴每秒1度,慢慢的旋转物体
        transform.Rotate(Vector3.right, Time.deltaTime);

            // ... at the same time as spinning it relative to the global
            // Y axis at the same speed.
            //相对于世界坐标,围绕y轴每秒1度,慢慢的旋转物体
        transform.Rotate(Vector3.up, Time.deltaTime, Space.World);
    }
}   


image.png


评分

参与人数 1活跃度 +15 展开 理由
微凉. + 15 【给力】阅贴无数,楼主最强!

查看全部评分

2020-5-9 10:55:40  
 赞 赞 0

使用道具 登录

0个回答,把该问题分享到群,邀请大神一起回答。

CG 游戏行业专业问题

程序逻辑文章算法U3DUnityUnity3D
显示全部 11
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表