- Home /
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.
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.
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.
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;
}
}
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;
}
}
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.
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.
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.
Your answer
Follow this Question
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