Asked by Coder

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
public Rigidbody rb;
public float jumpForce;
public float speed;
public float moveX;
public float moveY;
void Start()
{
if (rb == null)
{
rb = GetComponent<Rigidbody>();
}
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
moveX = Input.GetAxis("Horizontal");
moveY = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * speed * moveY * Time.deltaTime);
transform.Translate(Vector3.right * speed * moveX * Time.deltaTime);
}
}

make rigid body based movement for going forward and left and right, and prevent double jumping.

All Answers 1

Answered by GPT-5 mini AI
Here's a drop-in replacement script that uses the Rigidbody for movement (forward/back and left/right) in FixedUpdate and prevents double-jumping with a ground check. It reads input in Update, applies velocity in FixedUpdate, and only allows jumping when grounded. It also uses a configurable LayerMask and raycast distance for a reliable ground check.

Copy this into your project (name the file Controller.cs) and attach it to your player GameObject that has a Rigidbody.

```csharp
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Controller : MonoBehaviour
{
public Rigidbody rb;
public float speed = 6f;
public float jumpForce = 6f;

// Input storage
private float moveX;
private float moveZ;

// Ground check
public LayerMask groundMask = ~0; // default: everything
public float groundCheckDistance = 0.15f;
private bool isGrounded;

void Awake()
{
if (rb == null) rb = GetComponent<Rigidbody>();
// Recommended Rigidbody settings:
// rb.constraints = RigidbodyConstraints.FreezeRotation; // if you don't want the body to tip over
// rb.interpolation = RigidbodyInterpolation.Interpolate;
// rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
}

void Update()
{
// Read inputs in Update
moveX = Input.GetAxis("Horizontal");
moveZ = Input.GetAxis("Vertical");

// Jump input - only when grounded
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}

void FixedUpdate()
{
// Construct movement vector relative to the player orientation
Vector3 desiredMove = (transform.right * moveX + transform.forward * moveZ) * speed;

// Preserve current vertical velocity (gravity, jumping)
Vector3 newVelocity = new Vector3(desiredMove.x, rb.velocity.y, desiredMove.z);

rb.velocity = newVelocity;
}

// Ground check using a short raycast downwards.
// You can adjust groundCheckDistance and groundMask in the inspector.
bool IsGrounded()
{
// Slightly offset the ray origin up so the ray can detect ground when exactly at the surface
Vector3 origin = transform.position + Vector3.up * 0.1f;
return Physics.Raycast(origin, Vector3.down, groundCheckDistance + 0.1f, groundMask);
}

// Optional: visualize ground check in the editor
void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Vector3 origin = transform.position + Vector3.up * 0.1f;
Gizmos.DrawLine(origin, origin + Vector3.down * (groundCheckDistance + 0.1f));
}
}
```

Notes and tips:
- Set rb.constraints to freeze rotation if you don't want the Rigidbody to tip over when colliding.
- Assign the Ground layer to your floor objects and set groundMask to only that layer for more reliable grounding.
- You can tweak jumpForce and groundCheckDistance in the inspector.
- Using rb.velocity for horizontal movement overrides some physics forces; if you want smoother force-based motion, use rb.AddForce for horizontal movement instead (but keep the grounded check for jumps).