Character Controller 角色控制 Physics物理系列之一

Character Controller 角色控制器

本文檔主要是對Unity官方手冊的個人理解與總結(其實以翻譯記錄爲主:>)
僅作爲個人學習使用,不得作爲商業用途,歡迎轉載,並請註明出處。
文章中涉及到的操作都是基於Unity2018.4版本
參考鏈接:https://docs.unity3d.com/Manual/class-CharacterController.html

The Character Controller is mainly used for third-person or first-person player control that does not make use of Rigidbody physics.
角色控制器主要用於在不使用剛體物理的前提下的第三人稱或第一人稱玩家,控制角色的普遍做法。
在這裏插入圖片描述

Properties Function
Slope Limit Limits the collider to only climb slopes that are less steep (in degrees) than the indicated value.
坡度限制 限制對碰撞體只能爬上坡度小於此值的斜坡
Step Offset The character will step up a stair only if it is closer to the ground than the indicated value. This should not be greater than the Character Controller’s height or it will generate an error.
臺階高度 角色只能爬上高於當前地面高度一定範圍內(此值)時纔會走上樓梯。這應該不大於角色控制器的高度,否則會產生錯誤
Skin width Two colliders can penetrate each other as deep as their Skin Width. Larger Skin Widths reduce jitter. Low Skin Width can cause the character to get stuck. A good setting is to make this value 10% of the Radius.
皮膚厚度 兩個碰撞體可以互相穿透到和它們的皮膚厚度一樣深的地方。較大的皮膚厚度減少抖動。較低的皮膚厚度會導致角色卡住。一個好的設置是使這個值爲半徑的10%。
Min Move Distance If the character tries to move below the indicated value, it will not move at all. This can be used to reduce jitter. In most situations this value should be left at 0.
最小移動距離 如果角色試圖移動的距離比此值低,它將完全不移動。這可以用來減少抖動。在大多數情況下,這個值應該保持爲0。
Center This will offset the Capsule Collider in world space, and won’t affect how the Character pivots.
中心點 在世界空間這將偏移膠囊碰撞體,但不會影響如何角色的中心點。
Radius Length of the Capsule Collider’s radius. This is essentially the width of the collider.
半徑 膠囊碰撞體的半徑長度。本質上這就是碰撞體的寬度
Height The Character’s Capsule Collider height. Changing this will scale the collider along the Y axis in both positive and negative directions.
高度 角色膠囊體碰撞體高度。改變這一點將使碰撞體沿Y軸向正反兩個方向縮放。

在這裏插入圖片描述

Details

The traditional Doom-style first person controls are not physically realistic. The character runs 90 miles per hour, comes to a halt immediately and turns on a dime. Because it is so unrealistic, use of Rigidbodies and physics to create this behavior is impractical and will feel wrong. The solution is the specialized Character Controller. It is simply a capsule shaped Collider which can be told to move in some direction from a script. The Controller will then carry out the movement but be constrained by collisions. It will slide along walls, walk up stairs (if they are lower than the Step Offset) and walk on slopes within the Slope Limit.
傳統的Doom風格的第一人稱控制未遵守在真實物理。這個角色以每小時90英里的速度奔跑,突然停了下來,然後在一個轉彎處轉彎。因爲它是如此的不真實,使用剛體和物理來創造這種行爲是不切實際的,而且會讓人覺得是錯誤的。解決方案是用專門的角色控制器。它只是一個膠囊形狀的碰撞體,可以從腳本設置的某個方向移動。控制器將執行運動,但會受到碰撞體的約束。它會沿着牆壁滑行,爬上樓梯(如果它們臺階夠低的話)和在一定坡度範圍內的斜坡上行走。

The Controller does not react to forces on its own and it does not automatically push Rigidbodies away.
控制器本身不會對力做出響應,也不會自動將剛體推開。

If you want to push Rigidbodies or objects with the Character Controller, you can apply forces to any object that it collides with via the OnControllerColliderHit() function through scripting.
如果希望使用角色控制器推送剛體或對象,可以通過角色控制器的腳本通過OnControllerColliderHit()函數將力應用到與之碰撞的對象上。

On the other hand, if you want your player character to be affected by physics then you might be better off using a Rigidbody instead of the Character Controller.
另一方面,如果你想讓你的玩家角色受到物理的影響,那麼你最好使用剛體而不是角色控制器。

Fine-tuning your character 微調角色

You can modify the Height and Radius to fit your Character’s mesh. It is recommended to always use around 2 meters for a human-like character. You can also modify the Center of the capsule in case your pivot point is not at the exact center of the Character.
你可以修改高度和半徑來適應你的角色的網格。對於類人角色,建議始終使用大約2米。你也可以修改膠囊的中心,以防你的中心點不在角色的模型原點。

Step Offset can affect this too, make sure that this value is between 0.1 and 0.4 for a 2 meter sized human.
臺階高度也會影響這個值,對於2米大小的人,確保這個值在0.1到0.4之間。

Slope Limit should not be too small. Often using a value of 90 degrees works best. The Character Controller will not be able to climb up walls due to the capsule shape.
坡度限制不宜過小。通常使用90度的值效果最好。由於膠囊的形狀,角色控制器將無法爬上牆壁。

Don’t get stuck 防止卡住

The Skin Width is one of the most critical properties to get right when tuning your Character Controller. If your character gets stuck it is most likely because your Skin Width is too small. The Skin Width will let objects slightly penetrate the Controller but it removes jitter and prevents it from getting stuck.
調整角色控制器時,皮膚厚度是最關鍵的屬性之一。如果你的角色卡住了,很可能是因爲你的皮膚厚度太小。皮膚厚度將允許對象稍微穿透控制器,但它消除了抖動,防止它卡住。

It’s good practice to keep your Skin Width at least greater than 0.01 and more than 10% of the Radius.
這是一個很好的實踐,讓你的皮膚厚度至少大於0.01,並且超過半徑的10%。

We recommend keeping Min Move Distance at 0.
我們建議保持最小移動距離爲0。

See the Character Controller script reference here

You can download an example project showing pre-setup animated and moving character controllers from the Resources area on our website.

Hints

  • Try adjusting your Skin Width if you find your character getting stuck frequently.
    如果你發現你的角色經常被卡住,試着調整你的皮膚厚度。
  • The Character Controller can affect objects using physics if you write your own scripts.
    如果您編寫自己的腳本,字符控制器可以使用物理去影響其他對象。
  • The Character Controller can not be affected by objects through physics.
    角色控制器不受物理對象的影響。
  • Note that changing Character Controller properties in the inspector will recreate the controller in the scene, so any existing Trigger contacts will get lost, and you will not get any OnTriggerEntered messages until the controller is moved again.
    請注意,在檢查器中更改角色控制器屬性將在場景中重新創建控制器,因此任何現有的觸發器關聯的都將丟失,在再次移動控制器之前,您不會收到任何OnTriggerEntered消息。
  • The Character Controller’s capsule used in queries such as raycast might shrink by a small factor. Queries therefore could miss in some corner cases, even when they appear to hit the Character Controller’s gizmo.
    在諸如raycast之類的查詢中使用角色控制器的膠囊可能會被一個小因子縮小。因此,在某些特殊情況下,查詢可能會丟失,即使它們看起來擊中了場景中的角色控制器圖標。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章