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
0
Question by nahom247 · Feb 28, 2017 at 07:44 AM · c#gameobjectfloatcontrol

Change game objects single axis value based on rotation of another gameobject

How would I be able to control the lateral movement of a game object (table) using another rotating game object (steering wheel)? I currently can control the rotation of the steering wheel but am having trouble figuring how I would make the connections for the table to move to the right when the steering wheel is turned clockwise and to the left when turned counter clockwise .

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by EpiFouloux · Feb 28, 2017 at 07:48 AM

May you bring a picture of the project? I'm not sure about what you are asking.

I think you just need to parent the table to the steering wheel (Drag the table inside the steering wheel) Then, whenever the wheel turns, the table will also do it.

Warning Parenting is a useful tool, and you probably already know it, but for rotations don't use non-uniform scaled objects.

If parenting is not the solution, You should then go through scripting, and I'll be glad to help.

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 nahom247 · Mar 13, 2017 at 01:31 PM 0
Share

alt text

$$anonymous$$y understanding of parenting is that the child object would just move in the same direction which isn't what I am going for so I think scripting would be the way. If you look at the attached picture I am trying to control the horizontal movement of the white table object to the left or right by rotating the purple wheel below it. So if I rotate the wheel clockwise the table moves to the right and if I rotate the wheel counter clockwise it moves to the left along a the x axis. Appreciate the help!

controltable.jpg (28.7 kB)
avatar image EpiFouloux nahom247 · Mar 14, 2017 at 01:39 AM 0
Share

Yes parenting is not the solution you need to go scripting, with transfor$$anonymous$$g the rotation value of the wheel to translate value for the box object !

That means it will work only in play mode, is that fine with you?

avatar image nahom247 EpiFouloux · Mar 15, 2017 at 01:40 AM 0
Share

Yeah it only working in play mode is fine. Appreciate the help!

avatar image
0

Answer by Drigomen · Mar 16, 2017 at 04:53 AM

You could create a "ControlledByWheel" component an attach it to your table. So, you must reference your wheel in the inspector and it should (barely) work.

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class ControlledByWheel : MonoBehaviour
 {
   public Transform wheel; // reference to the object you want be controlled by
   public float factor = 0.1f; // how much "fast" you move
   float lastRotation;
   Rigidbody body;
 
   void Start()
   {
     body = GetComponent<Rigidbody>();
     lastRotation = wheel.eulerAngles.z;
   }
   void Update()
   {
     if (wheel == null) return;
     float deltaRotation = wheel.eulerAngles.z - lastRotation; // EVIL!!
     deltaRotation = ClampedRotation(deltaRotation);
     if (deltaRotation != 0f)
     {
       // Move the attached Rigidbody to Left/Right matching the change in
       // rotation of the wheel object
       var deltaPosition = Vector3.left * (factor * deltaRotation);
       body.MovePosition(transform.position + deltaPosition);
       lastRotation += deltaRotation;
     }
   }
 
   // A silly try to compensate EVILNESS of transform.eulerAngles
   // Only works for deltaRotation < 180
   float ClampedRotation(float angle)
   {
     angle %= 360f;
     if (angle >= 180f)
     {
       angle -= 360f;
     }
     else if (angle <= -180f)
     {
       angle += 360f;
     }
     return angle;
   }
 }

Note 1: transform.eulerAngles is EVIL! It accepts and/or returns angles clamped/non-clamped by 360, but never the way you expect or desire.
Note 2: I assumed you want a RigidBody in your object in order to move it (recommended). In this case, consider making it kinematic too. But if you want to, you can use increase "transform.position".

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

8 People are following this question.

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I move one of many instantiated gameobjects ONLY if it's currently selected in a list? 1 Answer

Making changes to a terrian in game 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