码迷,mamicode.com
首页 > 编程语言 > 详细

unity对象的简单平移与旋转

时间:2016-07-14 13:27:52      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

物体旋转问题,比如说自转,绕转,点击一物体,让其它物体绕该物体转,还有始终面向鼠标或者是player朝向等等,实际上都是一些简单的Rotate\RotateAround\LookAt等的属性,实现起来很简单,只需要获取场景里的物体,设置相对应的属性就可以。
技术分享 
 
技术分享 
 
1.场景中物体获取方法:
[C#] 纯文本查看 复制代码
 
1
2
cube = GameObject.FindGameObjectWithTag(Tags.Cube).transform;
                cube1 = GameObject.FindGameObjectWithTag(Tags.Cube1).transform;
                cube2 = GameObject.FindGameObjectWithTag(Tags.Cube2).transform;

 

2.控制移动:
[C#] 纯文本查看 复制代码
 
1
2
3
//控制移动
                float h = Input.GetAxis ("Horizontal");
                float v = Input.GetAxis ("Vertical");
                GetComponent<Rigidbody> ().MovePosition (transform.position - new Vector3 (h, 0, v) * speed * Time.deltaTime);
 
3.始终朝向鼠标方向的实现:
[C#] 纯文本查看 复制代码
 
1
2
3
4
5
6
7
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                //控制转向,朝向鼠标
                RaycastHit hitInfo;
                if (Physics.Raycast(ray, out hitInfo, 200, groundLayerIndex)) {
                                Vector3 target = hitInfo.point;
                                target.y = transform.position.y;
                                transform.LookAt(target);
                }

 

        平移是通过改变position的方法来实现的,通过设定值与deltaTime的乘积来控制速度,简单实用
        自转是Rotate,绕转是RotateAround,通过光线投放碰撞检测出被作为中心的物体,如果数量比较大可以用数组等等的方式来实现
 
4.点击某物体,其它物体围绕其转
[C#] 纯文本查看 复制代码
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//鼠标左键点击围绕旋转
                RaycastHit hit; 
                if (Input.GetMouseButton (0)) {
                        //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                        //RaycastHit hit; 
                        if (Physics.Raycast (ray, out hit)) {
                                Debug.Log (hit.collider.name);
                                //print("hello !");
                                //cube旋转
                                if (hit.collider.name == "Cube") {
                                                cube.Rotate (0, 1, 0);
                                                cube1.RotateAround (cube.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
                                                cube2.RotateAround (cube.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
                                }
                                if (hit.collider.name == "Cube1") {
                                                cube1.Rotate (0, 1, 0);
                                                cube.RotateAround (cube1.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
                                                cube2.RotateAround (cube1.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
                                }
                                if (hit.collider.name == "Cube2") {
                                                cube2.Rotate (0, 1, 0);
                                                cube1.RotateAround (cube2.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
                                                cube.RotateAround (cube2.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
                                }
                        
                }
        这几行是用来实现物体始终朝向鼠标方向的代码,像是合金弹头里角色的枪始终朝向鼠标类似。
总结:
        移动旋转等的原理其实很简单,还有一个addforce的方法,像是模拟子弹应该就是这种方法,但是模拟导弹轨迹这类应该都是setPosition,在实际的操作中可以根据需求来具体实现所需的逻辑结构。

unity对象的简单平移与旋转

标签:

原文地址:http://www.cnblogs.com/yushengqi/p/5670020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!