Sunday, November 24, 2024
spot_img

unity – I’m attempting to maneuver my sphere alongside a curved path made utilizing spline. The issue i’m going through is that the balls velocity when i apply torque is much less


I’m attempting to maneuver my ball alongside a curved path utilizing torque on the inflexible physique. The issue that i’m going through is that the balls velocity when i’m making use of torque is admittedly much less, it strikes actually slowly regardless of how a lot i improve the pace of the sphere. When i add power to it, every part is okay till it reaches the purpose the place the power turns into perpendicular to the airplane and it stops transferring regardless of how a lot power is utilized. I additionally tried making use of velocity to the sphere however it did not work as anticipated as when i transfer it alongside the curved platform, it goes up with none downside however when i launch the button, it comes down actually slowly like actually slowly. Right here is my code for controlling the ball.

utilizing UnityEngine;
utilizing UnityEngine.InputSystem;

public class BallMovement : MonoBehaviour
{
    [SerializeField] non-public float moveSpeed = 7f; //transfer pace of the sphere
    public Rigidbody rb;
    public int numberofStars = 0;
    non-public float currentSpeed;
    public float speedBoostDuration = 5f;
    public float newMoveSpeed = 200f;
    public Vector3 frc = new Vector3(100f, 100f, 0);
    non-public Vector2 inputVector = Vector2.zero;
    non-public IS_PlayerController playerController;
    non-public bool isOnGroundOrPlatform = false; //boolean variable in order that the ball solely strikes or adjustments path on floor or platform and never within the air

    void Begin()
    {
        currentSpeed = moveSpeed;
        rb = GetComponent();
    }

    non-public void FixedUpdate()
    {
        if (isOnGroundOrPlatform)
        {
            Vector3 dir = new Vector3(inputVector.x, 0f, inputVector.y);
            ApplyTorque(dir);
        }
    }

    non-public void Awake()
    {
        playerController = new IS_PlayerController();
    }

    non-public void OnEnable()
    {
        playerController.Allow();
        playerController.Sphere.Motion.carried out += OnMovementPerformed;
        playerController.Sphere.Motion.canceled += OnMovementCancelled;
    }

    non-public void OnDisable()
    {
        playerController.Disable();
        playerController.Sphere.Motion.carried out -= OnMovementPerformed;
        playerController.Sphere.Motion.canceled -= OnMovementCancelled;
    }

    non-public void OnMovementPerformed(InputAction.CallbackContext worth)
    {
        inputVector = worth.ReadValue();
    }

    non-public void OnMovementCancelled(InputAction.CallbackContext worth)
    {
        inputVector = Vector2.zero;
    }

    non-public void ApplyTorque(Vector3 path)
    {
        Vector3 torque = new Vector3(path.z, 0, -direction.x) * moveSpeed;
/*        rb.velocity = torque;
*/        rb.AddTorque(torque);
        /*        rb.AddForce(torque);  
        */
    }

    non-public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Floor") || collision.gameObject.CompareTag("Platform"))
        {
            isOnGroundOrPlatform = true;
        }
    }

    non-public void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("Floor") || collision.gameObject.CompareTag("Platform"))
        {
            isOnGroundOrPlatform = false;
        }
    }

    non-public void OnCollisionStay(Collision collision)
    {
        
        Debug.Log("Hey"+collision.gameObject.identify);
        if (collision.gameObject.CompareTag("Floor") || collision.gameObject.CompareTag("Platform"))
        {
            isOnGroundOrPlatform = true;
        }
    }
    non-public void OnTriggerEnter(Collider collider)
    {
        swap (collider.tag)
        {
            case "SpeedUP":
                StartCoroutine(SpeedBoost());
                break;
            case "JumpPad":
                rb.AddForce(frc, ForceMode.Impulse);
                break;
        }
    }

    non-public IEnumerator SpeedBoost()
    {
        moveSpeed = newMoveSpeed;
        yield return new WaitForSeconds(speedBoostDuration);
        moveSpeed = currentSpeed;
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles