物体的移动

物体的移动

在unity中,经常遇到的一个问题就是物体移动的问题,Unity引擎给出了很多种解决方案,这里先给大家介绍一种——Transform.Translate函数。

我们可以先建立一个小球,添加脚本

private float h=0.0f;
  private float v=0.0f;
 private transform tr;
 public float movespeed=10.0f;

  void strat(){
  tr=Getcomponent<Transform>();
 }
 void Update(){
 h=input.GetAxis("Horizontal");
 v=input.GetAxis("Vertical");
 //计算前后左右移动的向量
  vector3 movedir=(vector3.forward*v)+(vector3.right*h);
  //移动
  tr.translate(movedir*time.deltatime*movespeed);
   }


还有一种移动方式给移动的物体加上刚体,再给刚体加上力

    private float h=0.0f;
    private float v=0.0f;
   private Rigidbody rBody;
   public float movesforce=10.0f;
  
   void strat(){
   rBody=Getcomponent<Rigidbody>();
  }
  void Update(){
  h=input.GetAxis("Horizontal");
  v=input.GetAxis("Vertical");
 //计算前后左右移动的向量
  vector3 dir=new vector3(h,0,v)
  //移动
 rBody.AddForce(dir*time.deltatime*moveforce);

  }

给刚体加上速度

基本语法: public Vector3 velocity{get;set;}
功能说明: 此属性用于设置或返回刚体的速度值,其使用说明如下。
1、在脚本中无论是给刚体赋予一个Vector3类型的速度向量v,还是获取当前刚体的速度v,v的方向都是相对世界坐标系而言的。
2、velocity的单位是米每秒,而不是帧每秒,其中米是Unity默认的长度单位。

   private float h=0.0f;
        private float v=0.0f;
       private Rigidbody rBody;
       public float movesforce=10.0f;
      
       void strat(){
       rBody=Getcomponent<Rigidbody>();
      }
      void Update(){
      h=input.GetAxis("Horizontal");
      v=input.GetAxis("Vertical");
     //计算前后左右移动的向量
      vector3 dir=new vector3(h,0,v)
      //移动
     rBody.velocity=div*movesforce;
   
      }
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值