script for picking up objects error
Hi, i am beginner with scripting and i found this code on internet for picking up /throwing objects... I would like to make it work so i can use it as a example to learn javascript and see how things are done to make something similar.
But the code gives me following error. i tried searching but didnt found anything that fixed it.
And here is the code:
var player : GameObject;
var playcam : GameObject;
var grabbed : Transform;
var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;
var speed = 0.2;
var throwForce : float;
var throwRange : float;
var oldSensY = 15;
var oldSensX = 15;
var rotate : float;
private var vertRotAxis : Vector3;
private var horizontalRot : float;
private var verticalRot : float;
private var springJoint : SpringJoint;
function Start ()
{
}
function Update ()
{
rotate = Input.GetAxis("Rotate");
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
if (!Input.GetMouseButtonDown (0))
{
Camera.main.GetComponent("MouseLook").sensitivityX = oldSensX;
Camera.main.GetComponent("MouseLook").sensitivityY = oldSensY;
}
Screen.lockCursor = true;
var mainCamera = FindCamera();
// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 4))
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();
grabbed = springJoint.connectedBody.transform;
oldYRotCam = mainCamera.transform.eulerAngles.y;
oldYRotObj = springJoint.connectedBody.transform.eulerAngles.y;
oldYRotComp = oldYRotCam - oldYRotObj;
while (Input.GetMouseButton (0))
{
newYRotCam = mainCamera.transform.eulerAngles.y;
newYRot = newYRotCam - oldYRotComp;
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
Physics.IgnoreCollision(player.GetComponent.<Collider>(), grabbed.GetComponent.<Collider>());
yield;
if (Input.GetAxis("Rotate") == 0)
{
horzRot = springJoint.connectedBody.transform.InverseTransformDirection(Vector3.up).normalized;
springJoint.connectedBody.transform.Rotate(horzRot, Input.GetAxis("Mouse X") * 15);
}
if (distance > 5)
{
distance = 5;
}
if (distance < 2)
{
distance = 2;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance <= 5)
{
distance += speed;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance >= 2)
{
distance -= speed;
}
if (Input.GetAxis("Rotate") == 0)
{
mainCamera.GetComponent("MouseLook").sensitivityX = oldSensX;
mainCamera.GetComponent("MouseLook").sensitivityY = oldSensY;
player.GetComponent("MouseLook").sensitivityX = oldSensX;
}
if (Input.GetAxis("Rotate") != 0)
{
player.GetComponent("MouseLook").sensitivityX = 0F;
Camera.main.GetComponent("MouseLook").sensitivityX = 0F;
Camera.main.GetComponent("MouseLook").sensitivityY = 0F;
vertAxis = springJoint.connectedBody.transform.InverseTransformDirection(mainCamera.transform.TransformDirection(Vector3.right)).normalized;
springJoint.connectedBody.transform.Rotate(vertAxis, Input.GetAxis("Mouse Y") * 6);
horzAxis = springJoint.connectedBody.transform.InverseTransformDirection(mainCamera.transform.TransformDirection(Vector3.up)).normalized;
springJoint.connectedBody.transform.Rotate(horzAxis, -Input.GetAxis("Mouse X") * 6);
oldYRotCam = mainCamera.transform.eulerAngles.y;
oldYRotObj = springJoint.connectedBody.transform.eulerAngles.y;
oldYRotComp = oldYRotCam - oldYRotObj;
}
if (Input.GetMouseButtonDown (1))
{
held = 0;
Physics.IgnoreCollision(player.GetComponent.<Collider>(), grabbed.GetComponent.<Collider>(), false);
grabbed = null;
mainCamera.GetComponent("MouseLook").sensitivityX = oldSensX;
mainCamera.GetComponent("MouseLook").sensitivityY = oldSensY;
player.GetComponent("MouseLook").sensitivityX = oldSensX;
springJoint.connectedBody.AddExplosionForce(throwForce,mainCamera.transform.position,throwRange);
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
StopCoroutine ("DragObject");
yield;
}
}
if (springJoint.connectedBody)
{
Physics.IgnoreCollision(player.GetComponent.<Collider>(), grabbed.GetComponent.<Collider>(), false);
grabbed = null;
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
function FindCamera ()
{
if (GetComponent.<Camera>())
return GetComponent.<Camera>();
else
return Camera.main;
}
I dont see anyone fixing this. TL:DR situation with not enough info.
To learn Unity and coding go here
A null reference exception happens when an object isn't instantiated. In this script there are two possibilities for this: the player and playercam. Neither of these appear to be instantiated in the script, so you're supposed to link an object to them in the inspector. Did you do that?
Yes i did add objects inside the inspector..you can try it in your project and you will see..thats why i dont know what could be wrong
Answer by EmHuynh · Feb 15, 2016 at 07:21 PM
Hello, @Araw. The problem could be caused by your if statements in your Update function. The first and second if statements are the same if( !Input.GetMouseButtonDown( 0 ) )
the if the condition of the first if statement is true (which it will be), it will make a return
, causing the rest of your codes in the Update function to not execute. To resolve this issue, just remove the first if statement.
function Update ()
{
...
// If condition is true, it will return
if (!Input.GetMouseButtonDown (0))
return;
// If returned, the codes below this comment will not execute.
if (!Input.GetMouseButtonDown (0))
{
Camera.main.GetComponent("MouseLook").sensitivityX = oldSensX;
Camera.main.GetComponent("MouseLook").sensitivityY = oldSensY;
}
Screen.lockCursor = true;
var mainCamera = FindCamera();
...
}
Here's a mock-up of this problem:
function Test()
{
// If condition is true, it will call return (think exit).
if( !Input.GetMouseButtonDown( 0 ) ) {
Debug.Log( "Returning!" );
return; // Remove this statement and something different will happen.
}
// Won't get this far if returned.
if( !Input.GetMouseButtonDown( 0 ) ) {
Debug.Log( "Hello" );
}
Debug.Log( "World!" );
}
Your answer
Follow this Question
Related Questions
First person shooter : Leaning! 2 Answers
NavMesh of an automatically generated maze 0 Answers
example of new way to load levels 1 Answer
Menu object not responding 0 Answers
What is the C# " public void (name) " equivalent in JS? 1 Answer