unity控制物體移動
1. unity觸屏移動物體能不能控制物體移動的速度
通過Slider滑動條,試一試可不可以通過腳本控制速度。
2. U3D如何用腳本控制物體A移動到物體B的相對位置上
通過此方法將本地坐標轉化為世界即可;
即B.transform.TransformPoint(localposition);
3. unity3d 用WSAD控制物體移動的代碼
public void MoveCubeByInput ()
{
if (Input.GetKey (KeyCode.W)) {
this.gameObject.GetComponent <Transform> ().Translate (Vector3
.forward * Time.deltaTime * 3, Space.World);
}
if (Input.GetKey (KeyCode.A)) {
this.gameObject.GetComponent <Transform> ().Translate
(Vector3.left * Time.deltaTime * 5, Space.World);
}
if (Input.GetKey (KeyCode.S)) {
this.gameObject.GetComponent <Transform> ().Translate
(
Vector3.back * Time.deltaTime * 12, Space.World
);
}
if (Input.GetKey (KeyCode.D)) {
this.gameObject.GetComponent <Transform> ().Translate
(
Vector3.right * Time.deltaTime * 12, Space.World
);
}
if (Input.GetKey (KeyCode.Space)) {
this.gameObject.GetComponent <Transform> ().Translate
(
Vector3.up * Time.deltaTime * 12, Space.World
);
}
if (Input.GetKey (KeyCode.Q)) {
this.gameObject.GetComponent <Transform> ().Rotate (Vector3.up * Time.deltaTime * 50);
}
if (Input.GetKey (KeyCode.E)) {
this.gameObject.GetComponent <Transform> ().Rotate (Vector3.down * Time.deltaTime * 50);
}
然後在void update裡面調用
4. unity3d用鍵盤控制物體移動的工具
自己寫個腳本控制就好了
5. unity 怎麼讓物體自己移動
給物體綁定腳本,腳本名稱與物品命名保持一致
然後在腳本的update函數里,改變自身的屬性專就屬好了...
例如:
voidUpdate()
{
this.transform.Translate(Vector3.left*speed*Time.deltaTime);//transform獲取自身,Translate更新(狀態),this可省略(刷新速度過快??)
(*Time.deltaTime)防止Update調用過多
}
6. unity3d讓物體移動固定距離
1、打開unity3d創建一個「立方體」,作為要移動的物體。
7. unity3d如何控制一個物體移動到指定位置並且立刻停下來
public class PlayerMove : MonoBehaviour
{
public float speed = 5f; //移動時的速度
private Vector3 Player_dir; //主角的坐標
void Update ()
{
Player_dir.x = -Input.GetAxis("Horizontal") * speed * Time.deltaTime; //移動的數據
Player_dir.z = -Input.GetAxis("Vertical") * speed * Time.deltaTime; //移動的Z數據
this.transform.Translate(Player_dir.x, 0, Player_dir.z); //移動的距離
Player_dir = this.GetComponent<Transform>().position; //用來獲取當前主角的坐標
Exceed(); //檢測是否超出函數
}
void Exceed () //自定義超出函數
{
if (this.transform.position.x > 45) //檢測當前主角的X正半軸
{
this.transform.position = new Vector3(45, Player_dir.y, Player_dir.z);
}
else if (this.transform.position.x < -45) //檢測當前主角的X負半軸
{
this.transform.position = new Vector3(-45, Player_dir.y, Player_dir.z);
}
else if (this.transform.position.z < -45) //檢測當前主角的Z負半軸
{
this.transform.position = new Vector3(Player_dir.x, Player_dir.y, -45);
}
else if (this.transform.position.z > 45) //檢測當前主角的Z正半軸
{
this.transform.position = new Vector3(Player_dir.x, Player_dir.y, 45);
}
}
/*註:如果超出對它做出處理,重新指定坐標且這個坐標只能在四象軸范圍內。*/
}
8. Unity3D 幾個基本動畫(控制物體移動,旋轉
你可以使用2017新出timeline組件來實現移動和旋轉
如果是腳本代碼有:tranform.Translate
tranform.Rotate
或者使用回四元數答 Quaternion.LookAt把接收的值賦給transform的rotation
或者使用物理組件
rigidbody.AddForce
rigidbody.AddRelativeTorque
9. unity中,生成實例化後,如何控制它移動
privateGameObject_Player;
privatefloat_Speed=100;
(){
_Player=Instantiate(Resources.Load("實例化物體名"))asGameObject;
}
voidUpdate(){
if(Input.GetKeyDown(KeyCode.W))
{
_Player.transform.Translate(Vector3.forward*_Speed*Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.S))
{
_Player.transform.Translate(-Vector3.forward*_Speed*Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.A))
{
_Player.transform.Translate(Vector3.left*_Speed*Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.D))
{
_Player.transform.Translate(Vector3.right*_Speed*Time.deltaTime);
}
}
10. Unity3d 如何用按鈕控制物體前後左右移動
public var target:Transform;
public var moveSpeed=1;
function Start(){
if(!target){
("not set target!");
var go=GameObject.CreatePrimitive( PrimitiveType.Cube);
target=go.transform;
target.position=Camera.main.transform.TransformPoint(Vector3(0,0,5));
target.rotation=Camera.main.transform.rotation;
}
}
function OnGUI(){
var width=60;
var height=20;
GUI.BeginGroup(Rect((Screen.width-width*2)/2,Screen.height-height*3,width*2,height*3));
var moveDirection=Vector3.zero;
if(GUI.Button(Rect(width/2,0,width,height),"forward")){
moveDirection.z=1;
}
if(GUI.Button(Rect(width/2,height*2,width,height),"back")){
moveDirection.z=-1;
}
if(GUI.Button(Rect(0,height,width,height),"left")){
moveDirection.x=-1;
}
if(GUI.Button(Rect(width,height,width,height),"right")){
moveDirection.x=1;
}
if(target){
moveDirection=moveDirection*moveSpeed;
target.position=target.position+ target.rotation*moveDirection;
}
GUI.EndGroup();
}