Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created April 6, 2026 20:54
Show Gist options
  • Select an option

  • Save kurtdekker/6e5cc6604ff669d498606f3bcfb70bf1 to your computer and use it in GitHub Desktop.

Select an option

Save kurtdekker/6e5cc6604ff669d498606f3bcfb70bf1 to your computer and use it in GitHub Desktop.
simple physics ball roller
// uncomment this line if using new input system:
// #define USE_NEW_INPUT_SYSTEM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
// @kurtdekker
//
// - put this on a sphere
// - use arrow keys to move it
//
public class RollABallTiny : MonoBehaviour
{
[Header("Adjust to suit:")]
public float MoveForce;
public float JumpForce;
private void Reset()
{
MoveForce = 25;
JumpForce = 250;
}
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
if (!rb) rb = gameObject.AddComponent<Rigidbody>();
}
Vector3 commandForce;
bool jumpRequested;
void Update()
{
commandForce = Vector3.zero;
#if USE_NEW_INPUT_SYSTEM
// new system: InputAction, Keyboard.current, etc.
if (Keyboard.current.leftArrowKey.isPressed) commandForce.x = -1;
if (Keyboard.current.rightArrowKey.isPressed) commandForce.x = +1;
if (Keyboard.current.upArrowKey.isPressed) commandForce.z = +1;
if (Keyboard.current.downArrowKey.isPressed) commandForce.z = -1;
if (Keyboard.current.spaceKey.wasPressedThisFrame) jumpRequested = true;
#else
// old system: Input.GetKey, Input.GetAxis, etc.
commandForce = new Vector3(
Input.GetAxisRaw("Horizontal"),
0,
Input.GetAxisRaw("Vertical"));
if (Input.GetKeyDown(KeyCode.Space)) jumpRequested = true;
#endif
commandForce *= MoveForce;
}
private void FixedUpdate()
{
rb.AddForce(commandForce);
if (jumpRequested)
{
jumpRequested = false;
rb.AddForce(Vector3.up * JumpForce);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment