• 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
Question by JoySeeker · Jan 02, 2016 at 02:34 AM · rigidbodyhinge jointmotor

[FIXED] Changing the Target Velocity of a Hinge Joint Motor in script

I'm trying to get a Rigidbody to turn itself as it moves around a point by using a hinge joint motor. The only value I need to affect is the Target Velocity. When I initially tried this, I got an error telling me to store hingejoint.motor.targetvelocity as a temporary variable before altering it and reassigning it (getting it and setting it;) however, now that I've done that, I get no errors, but the target velocity remains how it was set before runtime. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class MerryGoRound : MonoBehaviour {
 
     public GameObject funnel;
     public float turnSpeed = 1f;
 
     HingeJoint joint;
     JointMotor motor;
 
     void Start () {
         joint = GetComponent<HingeJoint>();
         motor = joint.motor;
     }
     
     void FixedUpdate () {
         Vector3 line1 = funnel.transform.position - transform.position;
         Vector3 line2 = (funnel.transform.position + new Vector3(0f, 10f, 0f)) - transform.position;
         Vector3 perp = Vector3.Cross(line1, line2);
         Vector3 wheelForwardVector = transform.forward;
 
         float tempTargetVelocity = motor.targetVelocity;
         tempTargetVelocity = Vector3.Angle(wheelForwardVector, perp) * turnSpeed;
         motor.targetVelocity = tempTargetVelocity;
     }
 }
 
Comment
Adam_is_learning_all_the_time

People who like this

1 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 JoySeeker · Jan 02, 2016 at 10:34 PM 0
Share

Here's the code that works:

 using UnityEngine;
 using System.Collections;
 
 public class MerryGoRound : MonoBehaviour {
 
     HingeJoint myJoint;
     JointMotor myJointMotor;
     public float targVel = 2f;
 
     void Start () {
         myJoint = GetComponent<HingeJoint>();
     }
     
     void Update()
     {
         myJointMotor = myJoint.motor;
         myJointMotor.targetVelocity = targVel;
         myJoint.motor = myJointMotor;
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Tronyc · Dec 27, 2017 at 04:02 AM

This didn't work for me. I entered the code exactly as shown in the comment to your post, except I'm using motorSpeed instead of targetVelocity. Does that make a difference? I really need to be able to control the movement of the Joint.

Comment
Adam_is_learning_all_the_time

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

40 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

Related Questions

Hinge Joint Doesn't Move Unity 2018.4.17f1 0 Answers

Why Rigidbody.AddForce is not acting like the correct physical force? 1 Answer

Breakout Game Tutorial Question - Putting addForce in FixedUpdate makes the ball fly out of the screen at high speed? 1 Answer

RayCastHit Not Returning Rigidbody 0 Answers

Calculating the resting position of a rigidbody. 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