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 /
avatar image
1
Question by IanMancuso · Jul 06, 2017 at 01:39 AM · scripting problemphysics2d-physicshingejoint2dmotor

Cannot change 2d motor speed from script

I am getting this error in the console:

GetComponent requires that the requested component 'JointMotor2D' derives from MonoBehaviour or Component or is an interface. UnityEngine.GameObject.GetComponent[JointMotor2D] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:38) Paddle.Start () (at Assets/Paddle.cs:12)

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Paddle : MonoBehaviour {
     private Rigidbody2D rb2d;
     private JointMotor2D motor;
     public float speed = -100;
     // Use this for initialization
     void Start () {
         rb2d = GetComponent<Rigidbody2D> ();
         motor = GetComponent<JointMotor2D> ();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown ("left"))
             motor.motorSpeed = speed;
     }
 
     void FixedUpdate ()
     {
         
         
     }
 }

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jwizard93 · Jul 06, 2017 at 03:03 AM

I think you may have this all wrong. A JointMotor2D is a component of a component. So if you have a reference to a joint type component then you can say

 motorRef = jointRef.motor;

You can't even call it "motor" that would be illegal.

motorSpeed is just the speed you want a rigidbody that is connected to the joint to reach if the joint is moving.

So you need to get an object other than the objectyou want to move. Add a joint to it. Then in the inspector for that joint you can add your other object that has a rigidbody to it's connected rigidbody field. Now when you want that thing to move you can move the joint instead. The motorSpeed will limit how fast that thing will actually move.

I think you should search tutorials on joints and motors.

Comment
Add comment · Show 1 · 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 IanMancuso · Jul 07, 2017 at 11:27 AM 0
Share

Thank you, so, I know that the speed only sets a max speed, and I have the force set in the inspector. What I'm trying to do is change the max speed when I press a button.

This is my new code, it still does not work:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Paddle : $$anonymous$$onoBehaviour {
     private Rigidbody2D rb2d;
     private HingeJoint2D jointRef;
     private Joint$$anonymous$$otor2D motorRef;
     public float speed = -100;
     // Use this for initialization
     void Start () {
         rb2d = GetComponent<Rigidbody2D> ();
         jointRef = GetComponent<HingeJoint2D> ();
         motorRef = jointRef.motor;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.Get$$anonymous$$eyDown ("left"))
             motorRef.motorSpeed = speed;
     }
 
     void FixedUpdate ()
     {
         
         
     }
 }
 

Am I any closer?

avatar image
1

Answer by E_101 · Jan 19, 2018 at 06:53 AM

Ok, so I did some tinkering with the code you have so far and I got something close to what you are going for think. I am not sure how to directly change the Motor Speed from inside the script, however using ".maxTorque" and changing the speed variables when the are pressed down seems to work; especially when you change between ".useMotor = true" and ".useMotor = false" before each change.


I have yet to clean up the code, so there is probably a far simpler way to do this.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveLimbOnButtonDWN : MonoBehaviour {
 
     public Rigidbody2D rb2d;
     private HingeJoint2D jointRef;
     private JointMotor2D motorRef;
     public float speed = 200;
 
     void Start () {
 
         rb2d = GetComponent<Rigidbody2D> ();
         jointRef = GetComponent<HingeJoint2D> ();
         motorRef = jointRef.motor;
 
         jointRef.useMotor = false;
         print(motorRef.maxMotorTorque);
     }
 
     void Update () {
         
         if (Input.GetKey (KeyCode.A)) {
 
             motorRef.motorSpeed = speed;
             motorRef.maxMotorTorque = 100;
             jointRef.useMotor = true;
 
             print(motorRef.maxMotorTorque);

 //Trying to create opposite effect and stop movement (doesn't work as hoped)

         } if (Input.GetKeyUp (KeyCode.D)) {
 
             jointRef.useMotor = false;
             motorRef.maxMotorTorque = 0;
 
             print(motorRef.maxMotorTorque);
         }
     }
 }
 

Hopefully this has helped a bit. I am hoping someone else can make this even more streamlined for everyone.

Comment
Add comment · Show 1 · 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 IanMancuso · Feb 17, 2018 at 03:34 PM 0
Share

Thank you so much for replying to this after so long. I just tried the code, and It might be because i haven't touched this project in 6 months and forget most of what I did, but it doesn't seem to be working. my problem here is that I'm trying to make a paddle in pinball move, as a normal pinball machine would. If you have any other ideas as to how I could achieve that, I am completely open to ideas

avatar image
1

Answer by mohnadatyea12 · Sep 14, 2021 at 09:20 AM

Change the motorSpeed value this way:

  JointMotor2D motor = new JointMotor2D { motorSpeed = -30, maxMotorTorque = 100 };
  hingeJoint2d.motor = motor;,



Comment
Add comment · 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

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

145 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 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 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

How to make pinball flipper 0 Answers

Cannot set motor speed in script 1 Answer

How do I change 2d motor speed from a script? 1 Answer

HingeJoint2D disabling and re enabling limits, makes the joint rotate all the way back 0 Answers

Is it recommended to do scientific modelling in unity or should i write a standalone program 3 Answers


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