using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassPlayerMove:MonoBehaviour{//移动速度publicfloat _moveSpeed =5;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){//得到wsad的按下float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");//每帧的运行,实际上的速度要乘上 time.deltaTime,直接修改transform,也会考虑Rigidbody的碰撞了。
transform.position = transform.position +newVector3(h,0,v)}}
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassPlayerMove:MonoBehaviour{//移动速度publicfloat _moveSpeed =5;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){//得到wsad的按下float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");//调用Translate方法,里面是方向*速度*时间间隔
transform.Translate(newVector3(h,0,v)*_moveSpeed*Time.deltaTime);}}
using UnityEngine;using System.Collections;publicclass example : MonoBehaviour {voidFixedUpdate(){
rigidbody.AddForce(Vector3.up *10);}}// Adds a force upwards in the global coordinate system//在全局坐标系统添加一个向上的力
velocity直接修改速度,unity中的代码如下:
using UnityEngine;using System.Collections;publicclass example : MonoBehaviour {voidFixedUpdate(){if(Input.GetButtonDown("Jump"))//直接给一个向上的速度
rigidbody.velocity =newVector3(0,10,0);}}