Using a Xbox 360 joypad with the DRAG RIGID BODY SCRIPT

Hi All

I’m trying to work out how to use the 360 joypad with this script, need to use joystick button 13 to replace GetMouseButtonDown (0)) so we can press button and then be able to drag objects using the Sticks.

I thought just changing the GetMouseButtonDown (0)) to (Input.GetKeyDown (“joystick button 13”)) might work but sadly not that simple

Any help would be great :slight_smile:

Thanks
Andy

var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;

private var springJoint : SpringJoint;

function Update ()
{
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;

var mainCamera = FindCamera();

// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;

if (!springJoint)
{
var go = new GameObject(“Rigidbody dragger”);
body = go.AddComponent (“Rigidbody”);
springJoint = go.AddComponent (“SpringJoint”);
body.isKinematic = true;
}

springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
{
var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}

springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;

StartCoroutine (“DragObject”, hit.distance);
}

function DragObject (distance : float)
{
var oldDrag = springJoint.connectedBody.drag;
var oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
var mainCamera = FindCamera();
while (Input.GetMouseButton (0))
{
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
yield;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}

function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}

Use Input.GetButtonDown(“MyButtonName”), and set up MyButtonName (hopefully using a more descriptive name) in the input manager using “joystick button 13”.

–Eric

Thanks

Given that a try and no luck! even setting a keyboard key does not have an effect.

It seems the drag scritp has some issues

// Make sure the user pressed the mouse down
if (!Input.GetButtonDown (“Fire1”))
return;

^^
Set too Fire1 and still no Joy

Thanks
Andy

What is wrong with this code?

Fire1 being Prositive Button - joystick button 13, even the alt button set to k will not work…

var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;

private var springJoint : SpringJoint;

function Update ()
{
// Make sure the user pressed the mouse down
if (!Input.GetButtonDown (“Fire1”))
return;

var mainCamera = FindCamera();

// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;

if (!springJoint)
{
var go = new GameObject(“Rigidbody dragger”);
body = go.AddComponent (“Rigidbody”);
springJoint = go.AddComponent (“SpringJoint”);
body.isKinematic = true;
}

springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
{
var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}

springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;

StartCoroutine (“DragObject”, hit.distance);
}

function DragObject (distance : float)
{
var oldDrag = springJoint.connectedBody.drag;
var oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
var mainCamera = FindCamera();
while (Input.GetButtonDown (“Fire1”))
{
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
yield;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}

function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}

add a } to it :wink: your forgetting to do that and your going to the next function and getting errors because of it. Try and read your code before posting it :wink:

? have you looked at the full code above? that was just a snip to show the input.

Runs fine, just fails the pick-up the Joypad button

Thanks
Andy

opps :sweat_smile: sorry I didn’t see that last } but why are you even declaring the variables more than once a frame with the update function you create them and set them about 5 times a frame?

ok sorry about that last one I didn’t see that but my joystick a Logitech pro works fine and so I don’t see and thing wrong with your code besides declaring variables 5 times a frame. try a different joystick or put it online in a web-player and then let people with joysticks use it and see if it works for them because there appears nothing wrong with your code. (besides declaring variables 5 times a frame).

Hi Bud

This is the standard Drag that ships with Unity, i’m just bending this to would with the 360x, it’s odd as it looks right! but won’t work… most grim

Thanks for your help anyhow

Andy