unity学习之射线、角色控制器
2014-12-23 22:17
330 查看
unity学习,希望我的博客能给喜欢unity的朋友带来帮助
角色控制器(character controller)
角色控制器允许你在受制于碰撞的情况下很容易的进行运动,而不用处理刚体。角色控制器不受力的影响,仅仅当你调用Move函数时才运动。然后它将执行运动,但是受制于碰撞。
Unity3D封装了一个非常好用的组件来实现第一人称视角与第三人称视角游戏开发,我们称他为角色控制器组件,几乎不用写一行代码就可以完成一切的操作---- Charactr
Controller(角色控制器).
1、调用的方法是simplemove()使物体移动;
首先为物体添加charactercontroller(角色控制器);
2、角色控制器对象:private charactercontroller controller=null;
3、角色移动的速度:private float movespeed=30.0f;
给游戏对象添加角色控制器过程:
举例:
using UnityEngine;
using System.Collections;
public class Player2 : MonoBehaviour {
//如果用CharacterController(角色控制器),需要添加碰撞器
CharacterController controller;
public float speed=10;
void Start () {
controller=this.GetComponent<CharacterController>();
}
void Update () {
controller.SimpleMove (new Vector3(Input.GetAxis("Horizontal")*speed,0,Input.GetAxis("Vertical")*speed));
}
}
射线
射线,类比的理解就是游戏中的子弹。是在3D世界里中一个点向一个方向发射的一条无终点的线。在发射的过程中,一旦与其他对象发生碰撞,就停止发射。
射线包括两个元素:ray.origin(射线起点);ray.direction(射线的方向)
创建一个射线的方法:ray(origin : vector3,direction : vector3)
定义一个光线投射碰撞:raycasthit hit;
发射射线长度为:physics.raycast(ray,out hit,100);
打印射线:debug.drawline(ray.origin,jit.point);
举例:
using UnityEngine;
using System.Collections;
public class RayTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
RaycastHit hitInfo;
if(Physics.Raycast(ray,out hitInfo))
{
Debug.DrawLine(ray.origin,hitInfo.point);//划出射线,只有在scene视图中才能看到
GameObject gameObj = hitInfo.collider.gameObject;
Debug.Log("clic
4000
k object name is " + gameObj.name);
if(gameObj.tag == "boot")//当射线碰撞目标为boot类型的物品 ,执行拾取操作
{
Debug.Log("pick up!");
}
}
}
}
}
更多精彩请点击 http://www.gopedu.com/article
角色控制器(character controller)
角色控制器允许你在受制于碰撞的情况下很容易的进行运动,而不用处理刚体。角色控制器不受力的影响,仅仅当你调用Move函数时才运动。然后它将执行运动,但是受制于碰撞。
Unity3D封装了一个非常好用的组件来实现第一人称视角与第三人称视角游戏开发,我们称他为角色控制器组件,几乎不用写一行代码就可以完成一切的操作---- Charactr
Controller(角色控制器).
1、调用的方法是simplemove()使物体移动;
首先为物体添加charactercontroller(角色控制器);
2、角色控制器对象:private charactercontroller controller=null;
3、角色移动的速度:private float movespeed=30.0f;
给游戏对象添加角色控制器过程:
举例:
using UnityEngine;
using System.Collections;
public class Player2 : MonoBehaviour {
//如果用CharacterController(角色控制器),需要添加碰撞器
CharacterController controller;
public float speed=10;
void Start () {
controller=this.GetComponent<CharacterController>();
}
void Update () {
controller.SimpleMove (new Vector3(Input.GetAxis("Horizontal")*speed,0,Input.GetAxis("Vertical")*speed));
}
}
射线
射线,类比的理解就是游戏中的子弹。是在3D世界里中一个点向一个方向发射的一条无终点的线。在发射的过程中,一旦与其他对象发生碰撞,就停止发射。
射线包括两个元素:ray.origin(射线起点);ray.direction(射线的方向)
创建一个射线的方法:ray(origin : vector3,direction : vector3)
定义一个光线投射碰撞:raycasthit hit;
发射射线长度为:physics.raycast(ray,out hit,100);
打印射线:debug.drawline(ray.origin,jit.point);
举例:
using UnityEngine;
using System.Collections;
public class RayTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
RaycastHit hitInfo;
if(Physics.Raycast(ray,out hitInfo))
{
Debug.DrawLine(ray.origin,hitInfo.point);//划出射线,只有在scene视图中才能看到
GameObject gameObj = hitInfo.collider.gameObject;
Debug.Log("clic
4000
k object name is " + gameObj.name);
if(gameObj.tag == "boot")//当射线碰撞目标为boot类型的物品 ,执行拾取操作
{
Debug.Log("pick up!");
}
}
}
}
}
更多精彩请点击 http://www.gopedu.com/article
相关文章推荐
- unity学习之角色控制器
- Unity射线,角色控制器
- 【Unity 3D】学习笔记三十八:角色控制器
- unity之角色控制器和射线
- u3d学习之忽略碰撞、射线、角色控制器
- unity官方demo学习之Stealth(九)角色动画控制器
- 【Unity 3D】学习笔记三十八:角色控制器
- unity——使用角色控制器组件+射线移动
- 【Unity 3D】学习笔记四十:射线
- Unity手游之路<七>角色控制器
- 【Unity】11.1 角色控制器 (Character Controller)
- Unity手游之路角色控制器
- Unity角色控制器
- Unity3D学习之 ------------------------ "Hello Unity"控制角色移动
- unity官方demo学习之Stealth(十二)角色生命值
- 【Unity】4.4 添加角色控制器
- unity官方demo学习之Stealth(五)游戏控制器
- 射线和角色控制器
- 【Unity 3D】学习笔记四十:射线
- Unity Manual之CharacterController 角色控制器