1. 程式人生 > >Unity 角色控制器 移動監測碰撞

Unity 角色控制器 移動監測碰撞

		Vector3 movedirection = Vector3.zero;
		movedirection = Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
		CharacterController controller = GetComponent<CharacterController> ();
		//移動命令
		var flags = controller.Move (movedirection * Time.deltaTime);

		//CollisionFlags.CollidedBelow  監測底部放生碰撞  flags & CollisionFlags.CollidedBelow 返回1
		//CollisionFlags.CollidedSides  監測頂部發生碰撞  flags & CollisionFlags.CollidedSides 返回1
		//CollisionFlags.CollidedAbove    監測四周發生碰撞  flags & CollisionFlags.CollidedAbove 返回1

		bool grounded = (flags & CollisionFlags.CollidedBelow) != 0;