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 Giantbean · Sep 25, 2019 at 09:43 PM · quaterniontransform.positiontranslatetransform.rotation

translate a gameobjects position.y based on another gameobjects position .z

Sorry if this has been asked, I have been searching the forums and documentation for this but cant seem to find a solution.

I have a turret that looks up and down and left and right based on its rig following an aim constraint. I also have a joystick that I can grab and move around in a VR scene to drive the turret. The code below works to aim the turret left and right but I cant look up and down as the joystick moves on the x and z planes while the turret moves on the x and y planes.

So I need to some how translate the joysticks z movement into the turrets Y movement and i dont know how to do this?

current script:

 using UnityEngine;
 
 public class VRFlightStick : MonoBehaviour
     {
     
         public GameObject targetRectical;
         private Vector3 offset;
     
      private void Start()
         {
            offset = targetRectical.transform.position - this.transform.position;
         }
     
      private void Update()
         {  
             targetRectical.transform.position = this.transform.position + offset;
         }
     }
 


edit: Please excuse the pore quality mouse drawing but I thought a visual may help others understand the issue. alt text

I am trying to get the controller to move forward and backwards on the Z plane and have the turret move up and down on the y plane.

I have tried Quaternion.from to rotation, getting an origin x, y, and, z of the controller, and using if statements to see when the position changes and then update the transform of the turret as well as localEulerAngles and about 90 other lines of code that have not helped me get any closer then the code above. I have moved pivot points, and set up empty game objects, parenting and joints but I'm still not having any luck.

It seems like it should be simple enough but I have spent far to long on this and could really use some help. Thanks.

visualrep.png (391.9 kB)
Comment
Add comment · Show 3
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 WarmedxMints · Sep 25, 2019 at 09:49 PM 0
Share

Do you want to move or rotate the turret?

avatar image Giantbean WarmedxMints · Sep 26, 2019 at 06:15 PM 0
Share

I want to move the turrets targeting point on the X and Y plane. This will then rotate the turret based on its aim constraint. The script however should move not rotate.

avatar image Giantbean · Sep 27, 2019 at 03:10 PM 0
Share

Another attempt:

 using UnityEngine;
 
 public class Joystick : $$anonymous$$onoBehaviour
 {
     //This script is directly on the joystick and controlls a pivot point on the gun rather then the targeting rectical.
     //When you grab the VR joystick with this the gun rotates 90* to the left.
     //If the player turns left it lines back up.It can then be driven with up and down and side to side motion of the vive controller (not forward and back as needed).
 
         public GameObject AxesControlPoint;
 
     private Quaternion relativeRotation;
     private Vector3 offset;
 
     void Start()
     {
 
         offset = AxesControlPoint.transform.position - this.transform.position;
 
         relativeRotation = Quaternion.Inverse(this.transform.rotation) * AxesControlPoint.transform.rotation;
     }
 
     void Update()
     {
         AxesControlPoint.transform.localEulerAngles = this.transform.localEulerAngles;
     }
 
     private void AlignWithPlane(GameObject AxesControlPoint, float zOffset,
                          float xOffset = 0.0f, float yOffset = -0.0f)
 
     {
         AxesControlPoint.transform.position = new Vector3(this.transform.position.x + xOffset, // Away/towards plane
                                                   this.transform.position.y + yOffset, // Left/right plane
                                                   this.transform.position.z + zOffset); // Up/down plane
 
         AxesControlPoint.transform.rotation = this.transform.rotation;
     }
 }

1 Reply

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

Answer by WarmedxMints · Sep 27, 2019 at 01:05 AM

What I would do is create a controller for the stick which outputs a set range for -1 to 1 on each axis. You can then assign that output to move anything.

 using UnityEngine;
 
 public class VRFlightStick : MonoBehaviour
 {
     public enum Axis
     {
         x,
         y,
         z
     }
 
     [SerializeField]
     private float xMax = 0.1f, yMax = 0.1f;
 
     [SerializeField]
     private Axis _horizontal = Axis.x, _vertical = Axis.z;
 
     private Vector3 _centerPos;
 
     [SerializeField]
     private Vector2 _position;
 
     public Vector2 Position
     {
         get
         {
             return _position;
         }
     }
     
     private void Start()
     {
         _centerPos = transform.position;    
     }
         
     private void Update()
     {
         GetPos();    
     }
 
     private void GetPos()
     {
         var pos = _centerPos - transform.position;
 
         if (Mathf.Abs(pos[(int)_horizontal]) > xMax)
             xMax = Mathf.Abs(pos[(int)_horizontal]);
 
         if (Mathf.Abs(pos[(int)_vertical]) > yMax)
             yMax = Mathf.Abs(pos[(int)_vertical]);
 
         //The convert Range method makes sure we output a range of -1 to 1 on each axis.
         _position.x = ConvertRange(pos[(int)_horizontal], -xMax, xMax, -1, 1);
         _position.y = ConvertRange(pos[(int)_vertical], -yMax, yMax, -1, 1);
     }
 
     private float ConvertRange(float x, float in_min, float in_max, float out_min, float out_max)
     {
         return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
     }
 }

Then for your rectical

 public class TargetRectical : MonoBehaviour
 {
     public VRFlightStick FlightStick;
 
     public float MovementMultipler = 2f;
     private Vector3 _startPos;
 
     private void Start()
     {
         _startPos = transform.position;
     }
     public void Update()
     {
         if (FlightStick == null)
             return;
 
         var pos = _startPos;
 
         pos.x += FlightStick.Position.x * MovementMultipler;
         pos.y += FlightStick.Position.y * MovementMultipler;
 
         transform.position = pos;
     }
 }
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 Giantbean · Sep 27, 2019 at 02:32 PM 0
Share

Thanks. I am trying this out but so far its only making the turret jittery.

I put the first script directly on the VR in game joystick as well as trying it on a floating point above the joystick. I also tried putting the second script on the ai$$anonymous$$g rectical parented and unparented as well as placing it on the turret.

No dice. It just jitters each time I try it. I will keep playing with it though.

Thanks.

avatar image WarmedxMints Giantbean · Sep 27, 2019 at 02:51 PM 0
Share

You will need to set the x and y max. This is the distance from the centre to maximum x and maximum y positions. You can also add a tolerance to the movement so it outputs 0 if within a certain range. Gampads tend to have a 20% tolerance by default.

avatar image Giantbean WarmedxMints · Sep 27, 2019 at 05:56 PM 0
Share

I'm not using a game pad. I am using a Vive controller in VR grabbing a virtual joystick. I played with the $$anonymous$$ max earlier but I will take another look. Thanks again.

Show more comments

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

114 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

Related Questions

Vertical Camera Orbit 0 Answers

Align Parent object using child object as point of reference 3 Answers

Quaternion Help with 3D Planetary Motion around fixed pos Earth 0 Answers

Resetting Ball Object to the original parent and position 2 Answers

trying to take my door script and apply to a treasure chest 0 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