Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by walter11 · Nov 19, 2017 at 02:15 PM · effectball

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: alt text

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

unity.jpg (39.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to do a heat-haze effect in unity (FREE)? 1 Answer

Create wind over sand effect! 2 Answers

Help with my Light Flicker Script... 1 Answer

How would i write a script to add a Particle system to an exhaustust 1 Answer

Invisibility/warp effect 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges