Carrying physical game objects in javascript scripting problems with Unity 5.
Currently, I'm making a small game in unityscript, and I have a character with the standard first person controller script. Problem is, when I use the following .js script, a object that is being carried goes out of control. Floating around, to spinning in a type of motion like it's in a invisible tornado. And for the record this is in unity 5.3.3.
Here is the small script.
var onhand : Transform;
function Update() {
}
function OnMouseDown () {
GetComponent.<Rigidbody>().useGravity = false;
this.transform.position = onhand.position;
this.transform.parent = GameObject.Find("FPSController").transform;
this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
}
function OnMouseUp () {
this.transform.parent = null;
GetComponent.<Rigidbody>().useGravity = true;
}
I did try using GetButtonDown as well, with no success. Bare in mind, the GameObject that is suppose to be carried does have a rigidbody. And the player does have one as well.
How can this problem be solved?
Thanks.
Also, I keep getting error messages during runtime. I'm not 100% sure if it's related to the pickup script, but I don't understand why it does this when audio isn't needed for the FirstPersonController script.
IndexOutOfRangeException: Array index is out of range.
UnityStandardAssets.Characters.FirstPerson.FirstPersonController.PlayFootStepAudio () (at Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/FirstPersonController.cs:172)
UnityStandardAssets.Characters.FirstPerson.FirstPersonController.ProgressStepCycle (Single speed) (at Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/FirstPersonController.cs:159)
UnityStandardAssets.Characters.FirstPerson.FirstPersonController.FixedUpdate () (at Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/FirstPersonController.cs:130)
Don't understand how I could write a separate, or more simplified controller script. Given my limitations with javascript.
Answer by ZefanS · Apr 06, 2016 at 02:49 AM
From my own experience, one solution I would recommend would be making the rigidbody kinematic. This should prevent it from doing weird stuff, but does have some drawbacks as well. Test out this version and see if it works how you want:
var onhand : Transform;
function OnMouseDown()
{
GetComponent.<Rigidbody>().useGravity = false;
GetComponent.<Rigidbody>().isKinematic = true;
this.transform.position = onhand.position;
this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
}
function OnMouseUp()
{
this.transform.parent = null;
GetComponent.<Rigidbody>().useGravity = true;
GetComponent.<Rigidbody>().isKinematic = false;
}
As for the errors, are the footstep sounds assigned in the inspector for the First Person Controller Script? If you don't want them to sound you can always put the Audio Source volume to zero.
Hope this helps!
This might work if you want to have separate clicks to pick up and drop the item.
var onhand : Transform;
var held : boolean;
function Awake()
{
held = false;
}
function On$$anonymous$$ouseDown()
{
if (held == false)
{
GetComponent.<Rigidbody>().useGravity = false;
GetComponent.<Rigidbody>().is$$anonymous$$inematic = true;
this.transform.position = onhand.position;
this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
held = true;
}
else
{
this.transform.parent = null;
GetComponent.<Rigidbody>().useGravity = true;
GetComponent.<Rigidbody>().is$$anonymous$$inematic = false;
held = false;
}
}
Honestly though, I think it would be better to migrate to a model where the player handles the picking up rather than the object being picked up, as in: the player has a pickup script attached and everything is handled from the player's perspective. Just my opinion though, as there are many ways it could be done.
Have you tried deleting and adding a new First Person Controller prefab?
Honestly though, I think it would be better to migrate to a model where the player handles the picking up rather than the object being picked up, as in: the player has a pickup script attached and everything is handled from the player's perspective. Just my opinion though, as there are many ways it could be done.
If I did would this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
be replaced with a transform.child
? If so it would mean that this.transform.parent = null;
would have a .child as well?
How do you deter$$anonymous$$e what object the player will pick up when they click the mouse?
Answer by thePaintBrush · Apr 12, 2016 at 01:47 AM
Hi again ZefanS , for some reason code sample option has been acting up so doesn't look right. But when I tried your method the player couldn't hold on to the object when they let go of the left mouse button.
As for GetButtonDown(again code sample isn't working in this reply), been there done that, didn't work.
function Update() {
if (Input.GetButtonDown("Press E"))
GetComponent.<Rigidbody>().useGravity = false;
GetComponent.<Rigidbody>().isKinematic = true;
this.transform.position = onhand.position;
this.transform.parent =
GameObject.Find("FirstPersonCharacter").transform;
}
"As for the errors, are the footstep sounds assigned in the inspector for the First Person Controller Script? If you don't want them to sound you can always put the Audio Source volume to zero."
Did that and still get the same errors.
Note, you shouldn't add follow up questions as Answers. Please use the Add comment function ins$$anonymous$$d.
Well you can format your code in comments and answers, so yes, regardless of how code is formatted. You could read the user guide if you want more explanation.