© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2024
C. HardmanGame Programming with Unity and C#https://doi.org/10.1007/978-1-4842-9720-9_40

40. Pulling and Pushing

Casey Hardman1  
(1)
West Palm Beach, FL, USA
 

In this chapter, we’ll make use of raycasting to allow the player to point their camera at a GameObject with a Rigidbody and hold left-click to pull the object toward them or right-click to push it away from them. Rather than pulling and pushing by moving the Transform directly, we’ll apply force to the Rigidbody so that the physics system handles the motion for us. This will demonstrate how to manipulate Rigidbodies by applying forces to them.

Script Setup

The pushing and pulling features will be implemented ...

Get Game Programming with Unity and C#: A Complete Beginner’s Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.