Search Unity

Billiard ball effect not working

Discussion in 'Scripting' started by walter11, Nov 19, 2017.

  1. walter11

    walter11

    Joined:
    Jun 17, 2017
    Posts:
    1
    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.

    C# Code:

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

    public class StickShoot : MonoBehaviour {

    1. private Vector3 hitPoint;
    2. public AudioSource AS;
    3. public AudioClip breakOut;
    4. public float gizmoRadius;
    5. public float force;
    6. // Use this for initialization
    7. void Start () {
    8. }
    9. // Update is called once per frame
    10. void FixedUpdate () {
    11. RaycastHit hit;
    12. if (Physics.Raycast (transform.position, transform.TransformDirection (-Vector3.forward), out hit)) {
    13. if (hit.collider.tag == "WhiteBall") {
    14. hitPoint = hit.point;
    15. if (Input.GetKeyDown (KeyCode.Space)) {
    16. hit.rigidbody.AddForceAtPosition (force*hit.transform.forward,hit.point,ForceMode.Impulse);
    17. AS.PlayOneShot(breakOut);
    18. }
    19. }
    20. }
    21. }
    22. void OnDrawGizmos()
    23. {
    24. Gizmos.color = Color.red;
    25. Gizmos.DrawSphere (hitPoint, gizmoRadius);
    26. Gizmos.color = Color.blue;
    27. Gizmos.DrawLine (transform.position, hitPoint);
    28. }
    }
     

    Attached Files: