0
\$\begingroup\$

I am trying to move a ball along a curved path using torque on the rigid body. The problem that I am facing is that the ball's velocity when I am applying torque is really less, it moves really slowly no matter how much I increase the speed of the ball. When I add force to it, everything is fine until it reaches the point where the force becomes perpendicular to the plane and it stops moving no matter how much force is applied. I also tried applying velocity to the ball but it didn't work as expected as when i move it along the curved platform, it goes up without any problem but when I release the button, it comes down really slowly like really slowly. Here is my code for controlling the ball.

using System.Collections; using UnityEngine; using UnityEngine.InputSystem; public class BallMovement : MonoBehaviour { [SerializeField] private float moveSpeed = 7f; //move speed of the sphere public Rigidbody rb; public int numberofStars = 0; private float currentSpeed; public float speedBoostDuration = 5f; public float newMoveSpeed = 200f; public Vector3 frc = new Vector3(100f, 100f, 0); private Vector2 inputVector = Vector2.zero; private IS_PlayerController playerController; private bool isOnGroundOrPlatform = false; //boolean variable so that the ball only moves or changes direction on ground or platform and not in the air void Start() { currentSpeed = moveSpeed; rb = GetComponent<Rigidbody>(); } private void FixedUpdate() { if (isOnGroundOrPlatform) { Vector3 dir = new Vector3(inputVector.x, 0f, inputVector.y); ApplyTorque(dir); } } private void Awake() { playerController = new IS_PlayerController(); } private void OnEnable() { playerController.Enable(); playerController.Sphere.Movement.performed += OnMovementPerformed; playerController.Sphere.Movement.canceled += OnMovementCancelled; } private void OnDisable() { playerController.Disable(); playerController.Sphere.Movement.performed -= OnMovementPerformed; playerController.Sphere.Movement.canceled -= OnMovementCancelled; } private void OnMovementPerformed(InputAction.CallbackContext value) { inputVector = value.ReadValue<Vector2>(); } private void OnMovementCancelled(InputAction.CallbackContext value) { inputVector = Vector2.zero; } private void ApplyTorque(Vector3 direction) { Vector3 torque = new Vector3(direction.z, 0, -direction.x) * moveSpeed; /* rb.velocity = torque; */ rb.AddTorque(torque); /* rb.AddForce(torque); */ } private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Ground") || collision.gameObject.CompareTag("Platform")) { isOnGroundOrPlatform = true; } } private void OnCollisionExit(Collision collision) { if (collision.gameObject.CompareTag("Ground") || collision.gameObject.CompareTag("Platform")) { isOnGroundOrPlatform = false; } } private void OnCollisionStay(Collision collision) { Debug.Log("Hello"+collision.gameObject.name); if (collision.gameObject.CompareTag("Ground") || collision.gameObject.CompareTag("Platform")) { isOnGroundOrPlatform = true; } } private void OnTriggerEnter(Collider collider) { switch (collider.tag) { case "SpeedUP": StartCoroutine(SpeedBoost()); break; case "JumpPad": rb.AddForce(frc, ForceMode.Impulse); break; } } private IEnumerator SpeedBoost() { moveSpeed = newMoveSpeed; yield return new WaitForSeconds(speedBoostDuration); moveSpeed = currentSpeed; } } ``` 
\$\endgroup\$
2
  • \$\begingroup\$ torque is a rotation force, are you sure this is what you want? \$\endgroup\$ Commented Jun 10, 2024 at 10:41
  • \$\begingroup\$ It might help to add some screenshots or a gif, so we can see the curve, the ball, the plane, and how they're oriented/arranged with respect to one another — especially in the cases where it performs badly. Using Debug.DrawLine to visualize the input direction / torque vector may also help. \$\endgroup\$ Commented Jun 10, 2024 at 11:37

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.