- Home /
Billiard side spin effect not working
Helloo, i'm working in a billiard game and i have a problem. I try to make side spine effect in the ball but my way not works, the ball always goes forward. To do this I did a Raycast that starts from the tip of the stick and ends in contact with the ball. then I used Rigidbody.AddForceAtPosition and passed as parameters the forward direction of the ball and the hit.point of the Raycast on the ball.
Example image:
And this is my C# Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class StickShoot : MonoBehaviour {
private Vector3 hitPoint;
public AudioSource AS;
public AudioClip breakOut;
public float gizmoRadius;
public float force;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.TransformDirection (-Vector3.forward), out hit)) {
if (hit.collider.tag == "WhiteBall") {
hitPoint = hit.point;
if (Input.GetKeyDown (KeyCode.Space)) {
hit.rigidbody.AddForceAtPosition (force*hit.transform.forward,hit.point,ForceMode.Impulse);
AS.PlayOneShot(breakOut);
}
}
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere (hitPoint, gizmoRadius);
Gizmos.color = Color.blue;
Gizmos.DrawLine (transform.position, hitPoint);
}
}
I try everything but nothing works. Thanks for reading
Answer by Haseeb_BSAA · Jan 16 at 08:08 PM
First of all, I want to thank you for providing a working solution on how to achieve spin effect. I've been trying to achieve it for several days and this is the most reliable technique yet. Now in your code, the spin effect is produced well, but the ball always goes forwards. That's because you're hitting the ball at direction hit.transform.forward, which means it gets forward direction of ball and applies force in that direction. So even if you're aiming from whatever side, it always moves ball in the forward direction of the ball. To solve this, I simply made a clone of the ball, and I apply force on the forward of that clone cue ball. I made the clone follow the position of original cue ball (not rotation), and when I rotate my Cue Stick, I make sure that I also rotate the clone ball, so that the clone ball's forward is always aligned with the forward of the cue stick. Here's my code:
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.TransformDirection(Vector3.right), out hit, Mathf.Infinity))
{
Debug.DrawRay(transform.position, -transform.TransformDirection(Vector3.right) * hit.distance, Color.yellow);
if (hit.collider.tag == "cueBall")
{
hitPoint = hit.point;
if (Input.GetKeyDown(KeyCode.P))
{
StartCoroutine(waitFrames());
hit.collider.GetComponent<Rigidbody>().AddForceAtPosition(force * -normalPoint.transform.forward, hit.point, ForceMode.Impulse);
}
}
}
else
{
Debug.DrawRay(transform.position, -transform.TransformDirection(Vector3.right) * 1000, Color.white);
}
Here the normalPoint is game object of clone cue ball. Hope it helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612143031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i make a Character/object bounce using Javascript? 2 Answers
Propelling a Ball 1 Answer
Speed Cap for Rolling Ball 1 Answer
How do you make a shoot up feature like in dx-ball? 2 Answers
Platform falling script 1 Answer