iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1091229 免費的虛擬搖桿(成功)
by 黃國哲 2020-12-29 17:39:00, 回應(0), 人氣(482)


速度Velocity與施力Force的差別

使用下列程式碼,並套用在要移動的物件上


//////這邊是用一個速度移動,放開就會馬上停止

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class floatplayer : MonoBehaviour
{
    public float speed;
    public FixedJoystick fixedJoystick;
    public Rigidbody rb;

    public void FixedUpdate()
    {
     GetComponent<Rigidbody>().velocity = new Vector3(fixedJoystick.Horizontal * speed ,
GetComponent<Rigidbody>().velocity.y,fixedJoystick.Vertical * speed);
    }
}





/////下面為一個力,會不能煞車而是慣性向前


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    public float speed;
    public FloatingJoystick floatingJoystick;
    public Rigidbody rb;

    public void FixedUpdate()
    {
        Vector3 direction = Vector3.forward * floatingJoystick.Vertical + Vector3.right * floatingJoystick.Horizontal;
        rb.AddForce(direction * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);
    }
}

//////////////////////搖桿的方向就是角色面對的方向

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class 主角 : MonoBehaviour
{
    public float speed;
    public FixedJoystick fixedJoystick;
    public Rigidbody rb;


    public void FixedUpdate()
    {
    Vector3 direction = Vector3.forward * fixedJoystick.Vertical + Vector3.right * fixedJoystick.Horizontal;
    rb.transform.LookAt(new Vector3(transform.position.x + fixedJoystick.Horizontal, transform.position.y, transform.position.z + fixedJoystick.Vertical));
    }

}





將黃色的地方改成要使用的虛擬搖桿名稱
1.Fixed
2.Floating 
3.Dynamic
4.Variable 
有這四種




回應