問題描述
Unity Golf 遊戲物理和運動控制器 (Unity Golf game physics and movement controller)
所以,我有一個問題。我是一個團結的初學者,我想創建一個高爾夫遊戲。我可以編寫 C# 代碼,並且我在 3D 建模方面具有基本水平。我的問題是我不知道統一編碼是如何工作的。我的想法是一個類似於 Golf It 的系統,如果你按下鼠標左鍵,你有一個球桿會出現,然後它會獲取鼠標的 y 軸並將球桿綁定到該軸。一旦你擊球,它就會利用球桿的速度並將球傳送到高爾夫球上。物理在高爾夫中並不是那麼好,因為不管你打得多重,你都不能讓球飛起來。因此,我想實現一些逼真的高爾夫物理。(我在讀高中,因此我可以計算物理計算)。
我希望有人更有經驗,至少能給我一些如何解決這個問題的想法。已經謝謝了:)
順便說一句。我已經嘗試搜索教程,也自己嘗試過,但沒有得到任何有用的結果。但我至少已經建模了一個球桿和球,還有一個小高爾夫球場。
如果這有幫助的話,我想為高爾夫球編寫一個腳本,等待高爾夫球桿的衝動(是的當然不行)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controller : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (collision.gameObject.name == "golf_club")
{
rb.velocity = new Vector3(Collision.impusle, 0, 0);
}
}
}
參考解法
方法 1:
Welcome new user. Don't use Update. Unity has many functions for when "a collision happens".
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
You'll have to become really familiar with all those.
I suggest at first get familiar with using the physics engine, you'll be surprised how well it works for golf.
Later you can write your own physics (use a coroutine). You'll still likely use colliders only as triggers when you do that. You'll have to learn all about that too.
Be aware that it can take ~ 1,000 hours of work to become familiar with the basics of Unity.
(by lorinloewe4444、Fattie)