zoukankan      html  css  js  c++  java
  • 第三人称角色移动及自由移动视野(RigidBody实现)

    重点:向量的运算.在获得水平及垂直方向的速度之后,将方向进行重设,让方向与视野同步(即:相机的方向与人物方向相同)

    下面以一个实例来说明如何操作:

    1.如图创建一个地形(Terrain),两个正方体(Cube)(参照物),胶囊(Capsule)。把主摄像机放到Capsule下面当作子物体并且重置一下位置信息.

    (为了方便观察可以创建几个材质球给物体附上)

    2.把摄像机往后来在Game视图下可以达到如图效果即可:

    3.创建两个脚本一个用来控制移动另外一个控制视野转动:(我创建的move和freeLook两个脚本)

    move脚本内容:

    using UnityEngine;
    using System.Collections;
    
    public class move : MonoBehaviour {
    	public GameObject camer;//相机
    	// Use this for initialization
    	void Start () {
    		
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		Vector3 forward = camer.transform.TransformDirection (Vector3.forward);//记录相机前方向
    		Vector3 right=camer.transform.TransformDirection (Vector3.right);//记录相机右方向
    		float H = Input.GetAxis ("Horizontal");
    		float V = Input.GetAxis ("Vertical");
    		Vector3 he = H * right + V * forward;//将速度进行合成
    		GetComponent<Rigidbody> ().MovePosition (transform.position+ he * 5 * Time.deltaTime);//控制移动
    	}
    }
    

      freeLook脚本内容:

    using UnityEngine;
    using System.Collections;
    
    public class freeLook : MonoBehaviour {
    	public GameObject camer;
    	private float speed=5.0f;//转速
    	// Use this for initialization
    	void Start () {
    		
    	}
    	// Update is called once per frame
    	void Update () {
    		camer.transform.RotateAround (this.transform.position,Vector3.up,speed*Input.GetAxis("Mouse X"));//相机以人物为中心,自身Y轴进行旋转
    	}
    }
    

    4.把相机指定到脚本:  

  • 相关阅读:
    动态多条件查询分页以及排序(一)MVC与Entity Framework版url分页版
    关于MVC3种IOC自写方法GetItems("Model名字")得到的Model为空的解决方法
    css中!important 的使用
    MVc4阅读后总结
    jquery方法off()
    关于backgroundpostion 火狐和其他浏览器的不同
    Random.Next
    sort(function(a,b){return ab})是什么
    js 简单的自制分组(类似于分页) 结合mvc3
    Discus 论坛 使用方法
  • 原文地址:https://www.cnblogs.com/lichuangblog/p/6692989.html
Copyright © 2011-2022 走看看