I’m very new to Unity and I’m working on a game that player has to push and pull objects.I can push the objects but I don’t know how to make the player pull them. If anything I’m using CharacterController.
private void OnControllerColliderHit(ControllerColliderHit hit)
Rigidbody _rig = hit.collider.attachedRigidbody;
if (_rig != null && Input.GetKey (KeyCode.E))
Vector3 forceDirection = hit.gameObject.transform.position - transform.position;
forceDirection.y = 0;
_rig.AddForceAtPosition(forceDirection * pushForce, transform.position, ForceMode.Impulse);
Also I’m not sure if I will make the player push and pull in one direction at a time, but if I have to do it that way, I’d like to hear some tips about it.
The way I would do this is using the Configurable Joints or a Spring Joint component but, if you’re a beginner, it would take a bit of work. An alternative might be to simply apply a force in the opposite direction but you run the risk of having the object crash into you, something that wouldn’t happen with joints.
There are some tutorials on spring joints and, if you’re happy to have the “join” stretch, it’s a nice way to add physics since going round corners with a spring joint looks good.
As for pulling and pushing at the same time. Don’t worry. That’s what the Physics engine is there for. It will get it right. Remember it can cope with gravity, resistance, wind pressure, forces, explosions and all sorts of other things at the same time. Just add forces and keep clear of setting velocity or position manually, which might upset the internal calculations.