Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by thePaintBrush · Apr 03, 2016 at 01:35 AM · javascriptvarrigidbody physicsgetbuttondowntransform.parent

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thePaintBrush · Apr 03, 2016 at 09:19 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZefanS · Apr 12, 2016 at 01:59 AM 0
Share

@thePaintBrush

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?

avatar image thePaintBrush ZefanS · Apr 12, 2016 at 02:29 AM 0
Share

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?

avatar image ZefanS thePaintBrush · Apr 12, 2016 at 02:32 AM 0
Share

How do you deter$$anonymous$$e what object the player will pick up when they click the mouse?

Show more comments
avatar image
0

Answer by thePaintBrush · Apr 12, 2016 at 01:47 AM

@ZefanS

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZefanS · Apr 12, 2016 at 02:01 AM 0
Share

Note, you shouldn't add follow up questions as Answers. Please use the Add comment function ins$$anonymous$$d.

avatar image thePaintBrush ZefanS · Apr 12, 2016 at 02:08 AM 0
Share

Regardless of how code is formatted?

avatar image ZefanS thePaintBrush · Apr 12, 2016 at 02:12 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I can't get Input.GetButtonDown to return true when the specified button is pressed. 0 Answers

How to rotate cube2 if the script is in cube1? 1 Answer

How add(create) a new gameobject on scene on mouse button click. 2 Answers

Missing Reference. 0 Answers

how to make a level block/unblock 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges