Unity控制行走的代码!!1

这篇博客探讨了在Unity中控制角色行走的各种方法,包括使用NavMeshAgent组件、Transform.Translate、Rigidbody.velocity和Rigidbody.AddForce。通过示例代码,详细解释了如何根据玩家输入实现角色在3D环境中的平移移动。
摘要由CSDN通过智能技术生成

在这里插入图片描述

public float velcory = 10f;
private NavMeshAgent agent;
void Start()
{
agent = GetComponent();
}
//网上的控制移动的几种方法
void FixedUpdate()
{
float h = Input.GetAxis(“Horizontal”);
float v = Input.GetAxis(“Vertical”);
Vector3 vel = GetComponent().velocity;
//transform.Translate(new Vector3(-v * Time.deltaTime * velcory,transform.position.y, h * Time.deltaTime * velcory));
//m_rigidbody.velocity += new Vector3(h, 0, v);
//m_rigidbody.AddForce(new Vector3(h,0,v)*speed);
//m_rigidbody.MovePosition(new Vector3(-3.76f,0,1.58f));

    if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f)
    {
        this.GetComponent<Rigidbody>().velocity = new Vector3(-v * velcory, vel.y, h * velcory);//设置单位向量的速度和方向,使其移动
        //new Vector3(h, 0,v );//单位向量ADSW移动的位置的方向
        transform.rotation = Quaternion.LookRotation(new Vector3(-v, 0, h));//transform.rotation人物的朝向就是  单位向量ADSW移动的位置的方向
    }
    else //表示没有按下按键,h,v没有变化
    {
        if (agent.enabled==false)//并且没有在寻路的时候 
        {
            GetComponent<Rigidbody>().velocity = Vector3.zero;//把刚体的速度设置为0

        }           
    }
    if (agent.enabled)//如果在寻路
    {
        transform.rotation = Quaternion.LookRotation(agent.velocity);
        
    }
}

public float velcory = 10f;
private NavMeshAgent agent;
void Start()
{
agent = GetComponent();
}
//网上的控制移动的几种方法
void FixedUpdate()
{
float h = Input.GetAxis(“Horizontal”);
flo

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值