浏览代码

Character Controller moving

/main
Ciro Continisio 4 年前
当前提交
453789b2
共有 4 个文件被更改,包括 107 次插入16 次删除
  1. 25
      Assets/Scenes/CharController.unity
  2. 76
      Assets/Scripts/Protagonist.cs
  3. 14
      Assets/Materials/Protagonist.physicMaterial
  4. 8
      Assets/Materials/Protagonist.physicMaterial.meta

25
Assets/Scenes/CharController.unity


m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1961901422}
serializedVersion: 2
m_Mass: 1
m_Mass: 0.1
m_Constraints: 0
m_Constraints: 80
m_CollisionDetection: 0
--- !u!4 &1961901426 stripped
Transform:

propertyPath: inputReader
value:
objectReference: {fileID: 1724036687}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: jumpStrength
value: 5
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: airMomentumMultiplier
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: speed
value: 8
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240730, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: m_Material
value:
objectReference: {fileID: 13400000, guid: a16d1d43f142a5e46bd94366df8b3456,
type: 2}
- target: {fileID: 3341179906418240731, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: m_LocalPosition.x

76
Assets/Scripts/Protagonist.cs


public class Protagonist : MonoBehaviour
{
public InputReader inputReader;
private Rigidbody rigidbody;
public float speed = 10f;
public float airMomentumMultiplier = 3f;
public float jumpStrength = 1f;
private Vector3 momentum;
private bool isGrounded = true;
private bool jumpInitiated = false; //Becomes true on the frame that the jump button is pressed, consumed in FixedUpdate
private void OnEnable()
{
inputReader.jumpEvent += OnJump;
inputReader.moveEvent += OnMove;
//...
}
private void OnEnable()
{
inputReader.jumpEvent += OnJump;
inputReader.moveEvent += OnMove;
//...
}
private void OnDisable()
{
//...
}
private void OnDisable()
{
//...
}
private void Awake()
{
rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (isGrounded)
{
if(jumpInitiated)
{
//Jump!
//rigidbody.AddForce(Vector3.up * jumpStrength, ForceMode.Impulse);
rigidbody.velocity = new Vector3(momentum.x * speed, jumpStrength, momentum.z * speed);
jumpInitiated = false;
isGrounded = false;
}
else
{
//Normal movement
rigidbody.MovePosition(rigidbody.position + momentum * Time.fixedDeltaTime * speed);
}
}
else
{
//In midair
rigidbody.AddForce(momentum * airMomentumMultiplier * Time.fixedDeltaTime * speed);
}
}
momentum = new Vector3(movement.x, 0f, movement.y);
{
Debug.Log("JUMP");
}
}
{
if (isGrounded)
{
jumpInitiated = true;
}
}
private void OnCollisionEnter(Collision coll)
{
isGrounded = true;
rigidbody.velocity = Vector3.zero;
}
}

14
Assets/Materials/Protagonist.physicMaterial


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!134 &13400000
PhysicMaterial:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Protagonist
dynamicFriction: 0
staticFriction: 0
bounciness: 0
frictionCombine: 0
bounceCombine: 0

8
Assets/Materials/Protagonist.physicMaterial.meta


fileFormatVersion: 2
guid: a16d1d43f142a5e46bd94366df8b3456
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存