unity 簡單問題

本帖最後由 二師兄 於 2018-5-8 21:02 編輯

我想令到個球自轉, 要有physics, 在fixedupate 在用 AddTorque 但冇效, 有冇人知咩問題? 舊版code
  1. using UnityEngine;
  2. using System.Collections;

  3. public class ball_rotate_test : MonoBehaviour {

  4.     public Rigidbody rb;
  5.     public float amount = 50f;
  6.     public float h;
  7.     public float v;
  8.     // Use this for initialization
  9.     void Start () {
  10.         rb.maxAngularVelocity = 500f;
  11.         rb = GetComponent<Rigidbody>();
  12.     }

  13.     // Update is called once per frame
  14.     void Update () {
  15.         float h = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
  16.         float v = Input.GetAxis("Vertical") * amount * Time.deltaTime;
  17.     }

  18.     void FixedUpdate()
  19.     {
  20.         //transform.Rotate(new Vector3(0, 30, 0) * (float)0.5);


  21.         rb.AddTorque(transform.up * h);
  22.         rb.AddTorque(transform.right * v);

  23.         Debug.Log("FixedUpdate time :" + Time.deltaTime);

  24.     }
  25. }
複製代碼

原來要set default value.

TOP

你 Update 個 h 同 v 前面加左 float 等如 define local variable 而唔會反映響 FixedUpdate 果度

TOP

回覆 3# bennetng

試過唔得, h 同 v 要set default value

TOP

回覆 4# 二師兄
個 script 係咪你由零(or template) 開始寫定係用 tutorial 果 d sample code 改?

你 set default value 當然得但 Update 果段 code 係 suppose 個 game run 緊果陣用 keyboard 或 joystick 的左右去控掣個轉動力度, 但前提係唔好響前面加個 float 字

只係想自轉的話你其實可以成個 Update delete 左佢

TOP

回覆  二師兄
個 script 係咪你由零(or template) 開始寫定係用 tutorial 果 d sample code 改?

你 set d ...
bennetng 發表於 2018-5-8 22:35
  1.     using UnityEngine;
  2. using System.Collections;

  3. public class ball_rotate_test : MonoBehaviour {

  4.     public Rigidbody rb;
  5.     public float amount = 50f;
  6.     public float h;
  7.     public float v;
  8.     public bool iscol;
  9.     // Use this for initialization
  10.     void Start () {
  11.         gameObject.AddComponent<Rigidbody>();
  12.         rb.maxAngularVelocity = 200f;
  13.         rb = GetComponent<Rigidbody>();
  14.         }
  15.        
  16.         // Update is called once per frame
  17.         void Update () {

  18.         //h = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
  19.         //v = Input.GetAxis("Vertical") * amount * Time.deltaTime;
  20.     }

  21.     void FixedUpdate()
  22.     {
  23.         //transform.Rotate(new Vector3(0, 30, 0) * (float)0.5);
  24.         if (iscol == true) {
  25.         GetComponent<Rigidbody>().AddTorque(transform.up * h);
  26.         GetComponent<Rigidbody>().AddTorque(transform.right * v);
  27.         }

  28.         Debug.Log("FixedUpdate time :" + Time.deltaTime);

  29.     }

  30.     void OnCollisionEnter(Collision col)
  31.     {
  32.         h = 1;
  33.         v = 1;
  34.         iscol = true;
  35.     }
複製代碼
}

試緊咁, 其實我想掂到地就彈O個下就轉, 之後自然轉慢下來, 之後想掂到地就彈O個下就再轉, 其實可以點寫?

TOP

AddTorque 唔係咁用..

TOP

  1.         public float torque;
  2.         public Rigidbody rb;

  3.         void Start()
  4.         {
  5.                 rb = GetComponent<Rigidbody>();
  6.         }

  7.         void FixedUpdate()
  8.         {
  9.                 rb.AddTorque(transform.up * torque);
  10.         }
複製代碼
咁已經轉到.. 你想左轉/右轉.. 轉快D/慢D.. adjust torque個value就可以...

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

回覆 8# 7h1r733n


    點樣個ball 彈到最高點可以angular drag 漸加?

TOP

}

試緊咁, 其實我想掂到地就彈O個下就轉, 之後自然轉慢下來, 之後想掂到地就彈O個下就再轉, 其實可以點寫? ...
二師兄 發表於 2018-5-8 22:40


OnCollisionEnter 先AddTorque... 比幾大力.. 幾耐停.. 就睇Angular Drag同你真係比左幾多力落去..

TOP