I’m working on a ball racing game, and the player is controlled based on force vectors that move the ball in the direction that the player is pushing on either the joystick or arrow keys.
Normally, this works fine, but when the ball is knocked into the air, the player can still move the ball from side to side, which isn’t what I want. How do I adjust the script below so that the player cannot move from left to right while in the air?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour
{
private Rigidbody rb;
public float accelX = 1.0f;
public float accelY = 1.0f;
public float maxSpeed = 50.0f;
public Transform playerCam;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//Get player inputs
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
//Change inputs from world xyz to camera xyz
Vector3 camF = playerCam.forward;
Vector3 camR = playerCam.right;
camF.y = 0.0f;
camR.y = 0.0f;
camF = camF.normalized;
camR = camR.normalized;
//Add force to the ball based on camera position
Vector3 movement = ((camF * vertical * accelX) + (camR * horizontal * accelY));
rb.AddForce(movement);
//Speed limit
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
}
}