unity 第三人称控制对象平移转向C#脚本(亲测有效)
2017-03-04 15:21
1041 查看
using UnityEngine;
using System.Collections;
public class ControlMove : MonoBehaviour {
public float move_speed;
Animator animator;
public float turn_speed; //对象旋转的快慢控制
Rigidbody M_rigidbody; //对象身上绑定的刚体组件
private float V=0;
private float H=0;
void Start () {
animator = GetComponent<Animator>();
M_rigidbody = GetComponent<Rigidbody>();
}
//一定注意当对象旋转时,其局部坐标将改变,和世界坐标不一样了。。所以这里用到vector3.forward,其永远指向对象的前方即 Z轴
private void FixedUpdate()
{
V = Input.GetAxis("Vertical");
H = Input.GetAxis("Horizontal");
if (V!=0||H!=0)
{
Rotation(V, H);
animator.SetBool("stand_walk", true);
animator.SetBool("walk_stand", false);
transform.Translate(Vector3.forward*move_speed*Time.deltaTime);
}
else
{
animator.SetBool("walk_stand", true);
animator.SetBool("stand_walk", false);
}
}
//这里运用到四元数来进行对象的旋转
void Rotation(float vertical, float horizontal)
{
Vector3 targeDirection = new Vector3(horizontal,0f,vertical);
Quaternion targetRotation = Quaternion.LookRotation(targeDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(M_rigidbody.rotation, targetRotation, turn_speed * Time.deltaTime);
transform.rotation=newRotation;
}
}
using System.Collections;
public class ControlMove : MonoBehaviour {
public float move_speed;
Animator animator;
public float turn_speed; //对象旋转的快慢控制
Rigidbody M_rigidbody; //对象身上绑定的刚体组件
private float V=0;
private float H=0;
void Start () {
animator = GetComponent<Animator>();
M_rigidbody = GetComponent<Rigidbody>();
}
//一定注意当对象旋转时,其局部坐标将改变,和世界坐标不一样了。。所以这里用到vector3.forward,其永远指向对象的前方即 Z轴
private void FixedUpdate()
{
V = Input.GetAxis("Vertical");
H = Input.GetAxis("Horizontal");
if (V!=0||H!=0)
{
Rotation(V, H);
animator.SetBool("stand_walk", true);
animator.SetBool("walk_stand", false);
transform.Translate(Vector3.forward*move_speed*Time.deltaTime);
}
else
{
animator.SetBool("walk_stand", true);
animator.SetBool("stand_walk", false);
}
}
//这里运用到四元数来进行对象的旋转
void Rotation(float vertical, float horizontal)
{
Vector3 targeDirection = new Vector3(horizontal,0f,vertical);
Quaternion targetRotation = Quaternion.LookRotation(targeDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(M_rigidbody.rotation, targetRotation, turn_speed * Time.deltaTime);
transform.rotation=newRotation;
}
}
相关文章推荐
- Unity实现在安卓手机上触屏控制转向--控制对象为飞机
- unity 动态控制 组件或脚本的激活与关闭 对象的显示与隐藏
- Unity学习(七):Unity中游戏对象间的访问方法、脚本时间控制
- unity。。可指定目标点的类弓箭斜抛运动控制C#脚本(不采用物理引擎)。。纯位移旋转控制
- Unity 2D角色控制脚本C#直接上代码
- 【Unity3D入门教程】使用C#脚本控制游戏对象
- unity中实现的第三人称摄像机控制脚本。
- unity。。。完美简单解决靠鼠标在屏幕上的移动来控制相机水平,垂直旋转的c#脚本
- Unity用键盘控制物体左右旋转前后移动的c#脚本
- 【Unity】6.3 通过 C# 脚本创建和访问游戏对象
- Unity摇杆控制物体移动c#脚本
- 用unity来写小球转向后向目标移动 +c#脚本
- Unity 3D 控制物体前后左右均匀移动脚本
- Unity 3D视频播放器场景C#脚本
- unity3d 使用c#如何访问某个对象里的c#脚本里的一个变量
- Unity 3D 动态创建对象,并渲染贴图,动态添加删除脚本
- Unity 控制游戏对象,显示小地图
- Unity3D中的第三人称镜头的脚本控制
- Python脚本控制的WebDriver 常用操作 <十八> 获取测试对象的css属性
- Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容