Giter Club home page Giter Club logo

unity-billiards-trajectory's Introduction

Built with Unity 2019.3.7 with these extra packages:

  • ProBuilder
  • ProGrids (preview)

I exposed Inspector controls in the TrajectoryController script of the GameController object. Changing any of the force variables there in the Inspector will update the trajectory in the game view.

Controls:

  • Left-click on a ball to select it and see its trajectory
  • Left-click and hold to orbit the camera
  • Right-click to shoot the active ball

Overview

The videos I learned from used some practices that adversely affect performance which I wanted to avoid, and to do so the architecture I ended up with is a little different.

At the top is the GameController with individual scripts for handling ball selection (BallSelection), then scene management and player input (TrajectoryController).

The hidden physics scene is generated on TrajectoryController.Awake. If values in that script in the inspector are changed, it will update the active ball's trajectory.

The balls and the table have the SimulatedPiece script which creates a clone of itself, destroys or hides its renderers, and moves that hidden clone over to the hidden physics scene. Every time the trajectory changes or a new ball is selected, its Reset method will be called that moves the hidden ball back to where the visible ball is.

BallController on each ball takes care of updating the LineRenderer whenever SimulatedPiece.SimulateImpulse runs the simulation.

Why

I was building a billiards-style game when Unity 2018.3 was released and multi-scene physics along with it. Accurate trajectory prediction has always been a challenge but this feature greatly simplifies the solution using built-in physics.

In Unity's blog post there is a billiard ball video of the feature in action but as far as I know it was never released (UPDATE: released here), so thanks to some other user-made videos (this and this) I was able to put this solution together and decided to share it with everyone since I know there are others wondering as well.

I'm still a total novice to Unity so I'd appreciate pull requests to help me (and everyone looking at this) improve. However, I don't have the bandwidth to maintain this often so here's the forum thread that others might have answers for questions you may have.

unity-billiards-trajectory's People

Contributors

vonwolfehaus avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.