Skip to content

Instantly share code, notes, and snippets.

@Priler
Last active November 15, 2021 12:45
Show Gist options
  • Save Priler/03d7fe1893f179c9e23b632578ff87c3 to your computer and use it in GitHub Desktop.
Save Priler/03d7fe1893f179c9e23b632578ff87c3 to your computer and use it in GitHub Desktop.
CubeController.cs
using UnityEditor;
using UnityEngine;
using DG.Tweening;
/// <summary>
/// Attribute to select a single layer.
/// </summary>
public class LayerAttribute : PropertyAttribute
{}
[CustomPropertyDrawer(typeof(LayerAttribute))]
class LayerAttributeEditor : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
property.intValue = EditorGUI.LayerField(position, label, property.intValue);
}
}
[RequireComponent(typeof(Rigidbody))]
public class CubeController : MonoBehaviour
{
[SerializeField] private float moveSpeed = 15f; // cube movement speed, applied to Rigidbody.MovePosition
[SerializeField] private float jumpForce = 30f; // cube jump force, applied to Rigidbody.AddForce
[SerializeField] private float fallMultiplier = 10f; // determinates fall gravity multiplier (how fast a cube will fall after jump)
[SerializeField, Layer] int groundLayer = 0; // ground layer selector (instead of tags? :3)
[SerializeField, Range(1, 360)] private int spinAnimationAngle = 180; // (DG.Tweening) DORotate target angle
[SerializeField, Range(0.1f, 1.0f)] private float spinAnimationTime = 0.75f; // (DG.Tweening) DORotate target animation time
private Rigidbody _rb;
private bool _doJump = false; // used to transfer input from Update into FixedUpdate
private bool _isJumping = false; // determinates if a cube is currently jumping or not
void Awake()
{
_rb = transform.GetComponent <Rigidbody>();
}
void Update() {
// Unity built-in Input Manager
if(Input.GetButtonDown("Jump") && !_isJumping) {
Jump();
}
}
// FixedUpdate is called once per Fixed Timestep (50 times/second default)
void FixedUpdate()
{
// move the cube forward
Vector3 moveVector = Vector3.forward * moveSpeed * Time.deltaTime;
_rb.MovePosition(transform.position + moveVector);
// check for a jump input
if(_doJump) {
_rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
_doJump = false;
}
// Gravity fall multiplier
_rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
void OnCollisionEnter(Collision collision) {
// CompareTag is alot performant than equality-operator or TryGetComponent
// but still, it is much faster to use layers :3
// (if you interested in such micro optimization)
if(collision.gameObject.layer == groundLayer) {
if(Input.GetButton("Jump") && _isJumping) {
// Bunny Hop
Jump();
} else if(_isJumping) {
_isJumping = false;
}
}
}
void Jump() {
_doJump = true; // set flag for FixedUpdate
_isJumping = true; // set jump status
// some cool DG stuff :]
transform.DOComplete();
transform.DOShakeScale(.5f, .5f, 3, 30);
transform.DORotate(new Vector3(spinAnimationAngle, 0, 0), spinAnimationTime, RotateMode.LocalAxisAdd).SetRelative(true);
}
}
@Priler
Copy link
Author

Priler commented Aug 2, 2021

Hello Abraham, dont worry about your self, Roman is idiot, no reason to look to Roman he is Unity Noob Developer. But, why Jump Controller? Roman has really good video about this.

Best wishes,
Konstantin

Yeah, just renamed the file to "CubeController", makes more sense.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment