Home Game Development unity – How can I rotate an object based mostly on one other’s offset to it?

unity – How can I rotate an object based mostly on one other’s offset to it?

0
unity – How can I rotate an object based mostly on one other’s offset to it?

[ad_1]

The reply is definitely fairly straightforward when you do the mathematics. You have a hard and fast distance of Y and a variable distance of X (See Picture 1). You want to seek out out the angle between Z and X and switch your turret that rather more.
enter image description here

Step 1 – Get distance between the turret line (V) and the gun line (W) which is Y (that is fixed however does not damage to calculate). Get distance from the turret to the goal (which is X).

Step 2 – Divide Y by X after which get the Hyperbolic Sine of the worth

double turnRadians = Mathf.Asin(Y/X);
double angle = Mathf.Rad2Deg * turnRadians;

//the place B is the purple dot, A is a degree on the X line and C is a degree on the Z line.

Step 3 – Turn the turret that rather more (across the axis that goes from it is high to it is backside, most probably up axis however solely you’ll be able to know that half).

gameObject.rework.Rotate(Vector3.up, flipAngle);

Of course on this case, you want it to show counterclockwise so that you would possibly want so as to add a minus in entrance of the flipAngle there, as in -turnAngle.

Edited some components. Thanks to @ens for declaring the distinction in distance.

The OP mentioned his gun has an angle so right here we go, picture first, rationalization later:
enter image description here

We already know from the earlier calculation the place to purpose the purple line in accordance with the blue line. So aiming for the blue line first:

float flipAngle = angleBetweenTurretAndTarget - angleBetweenTurretAndGun;
turret.rework.Rotate(Vector3.up, flipAngle);

The solely calculation that differs right here, is the calculation of “X Prime” (X’) as a result of the angle between the gun and the turret (angle “a”) modified the space between the strains.

//(this half had a mistake of utilizing earlier code inside new variable names, YPrime and Y are proven as X' and X within the 2nd image.
float YPrime = Cos(a)*Y; //this half is what @ens is doing in his reply
double turnRadians = Mathf.Asin(YPrime/X);
double angle = Mathf.Rad2Deg * turnRadians;
turret.rework.Rotate(Vector3.up, angle);

This subsequent half is ONLY essential when you’re doing the turret weapons modular (i.e. consumer can change the weapons on a turret and completely different weapons have completely different angles). If you are doing this in editor, you’ll be able to already see what the gun angle is in accordance with the turret.

There are two strategies for locating the angle “a” , one is the rework.up technique:

float angleBetween = Vector3.Angle(turret.rework.up, gun.rework.up);

Above approach will calculate in 3D, so if you’d like a 2D consequence, you might want to do away with the Z axis (that is what I assume the place gravity is, however when you modified nothing, in Unity it is Y axis that’s up or down, i.e. gravity is on the Y axis, so that you may need to vary issues up):

Vector2 turretVector = new Vector2(turret.rework.up.x, turret.rework.up.y);
Vector2 gunVector = new Vector2(gun.rework.up.x, gun.rework.up.y);
float angleBetween = Vector2.Angle(turretVector, gunVector);

Second means is the rotation technique (I’m considering in 2D on this case):

double angleRadians = Mathf.Asin(turret.rework.rotation.z - gun.rework.rotation.z);
double angle = 2 * Mathf.Rad2Deg * angleRadians;

Again, all these codes offers you values which are optimistic, so that you may need so as to add or subtract the quantity relying on the angle (there are calculations for that too, however I’m not going to go that in-depth). A very good place to begin on this is able to be the Vector2.Dottechnique in Unity.

Final block of code for extra rationalization of what we’re doing:

//flip turret in the direction of goal
turretTransform.up = goalTransform.place - turretTransform.place;
//modify for gun angle
if (weaponTransform.localEulerAngles.z <180) //if the worth is over 180 it is truly a detrimental for us
    turretTransform.Rotate(Vector3.ahead, 90 - b - a);
else
    turretTransform.Rotate(Vector3.ahead, 90 - b + a);

If you probably did all the things proper, it’s best to get a scene like this (hyperlink for the unitypackage):
enter image description here
What I imply by all the time optimistic values:enter image description here

The Z technique can provide detrimental values:enter image description here

For an instance scene, get the unitypackage from this hyperlink.

Here’s the code I’ve used within the scene (on the turret):

public class TurretAimCorrection : MonoBehaviour
{
    public Transform goalTransform;
    public Transform turretTransform;
    public Transform weaponTransform;

    non-public float f, d, x, y, h, b, a, weaponAngle, flipAngle;
    non-public void Start()
    {
        TurnCorrection();
    }

    non-public void Update()
    {
        TurnCorrection();
    }
    void TurnCorrection()
    {
        //discover distances and angles
        d = Vector2.Distance(new Vector2(goalTransform.place.x, goalTransform.place.y), new Vector2(turretTransform.place.x, turretTransform.place.y));
        x = Vector2.Distance(new Vector2(turretTransform.place.x, turretTransform.place.y), new Vector2(weaponTransform.place.x, weaponTransform.place.y));
        weaponAngle = weaponTransform.localEulerAngles.z;
        weaponAngle = weaponAngle * Mathf.Deg2Rad;
        y = Mathf.Abs(Mathf.Cos(weaponAngle) * x);
        b = Mathf.Rad2Deg * Mathf.Acos(y / d);
        a = Mathf.Rad2Deg * Mathf.Acos(y / x);
        //flip turret in the direction of goal
        turretTransform.up = goalTransform.place - turretTransform.place;
        //modify for gun angle
        if (weaponTransform.localEulerAngles.z < 180)
            turretTransform.Rotate(Vector3.ahead, 90 - b - a);
        else
            turretTransform.Rotate(Vector3.ahead, 90 - b + a);
        //Please go away this remark within the code. This code was made by 
        //http://gamedev.stackexchange.com/customers/93538/john-hamilton a.okay.a. CrazyIvanTR. 
        //This code is offered as is, with no ensures. It has labored in native assessments on Unity 5.5.0f3.
    }
}

3D tailored code with X and Z because the 2D aircraft:

public class TurretAimCorrection : MonoBehaviour
{
    public Transform goalTransform; //drag goal right here
    public Transform turretTransform; //drag turret base or turret high half right here
    public Transform weaponTransform; //drag the hooked up weapon right here

    non-public float d, x, y, b, a, weaponAngle, flipAngle;
    non-public void Start()
    {
        TurnAdjustment();
    }

    non-public void Update()
    {
        TurnAdjustment();
    }
    void TurnAdjustment()
    {

        d = Vector2.Distance(new Vector2(goalTransform.place.x, goalTransform.place.z), new Vector2(turretTransform.place.x, turretTransform.place.z));
        x = Vector2.Distance(new Vector2(turretTransform.place.x, turretTransform.place.z), new Vector2(weaponTransform.place.x, weaponTransform.place.z));
        weaponAngle = weaponTransform.localEulerAngles.y;
        weaponAngle = weaponAngle * Mathf.Deg2Rad;
        y = Mathf.Abs(Mathf.Cos(weaponAngle) * x);
        b = Mathf.Rad2Deg * Mathf.Acos(y / d);
        a = Mathf.Rad2Deg * Mathf.Acos(y / x);
        //flip turret in the direction of goal
        turretTransform.ahead = new Vector3(goalTransform.place.x, 0, goalTransform.place.z) - new Vector3(turretTransform.place.x, 0, turretTransform.place.z);
        //modify for gun angle
        if (weaponTransform.localEulerAngles.y < 180)
            turretTransform.Rotate(Vector3.up, - a +b-90);
        else
            turretTransform.Rotate(Vector3.up, + a+ b - 90);
        //Please go away this remark within the code. This code was made by 
        //http://gamedev.stackexchange.com/customers/93538/john-hamilton a.okay.a. CrazyIvanTR. 
        //This code is offered as is, with no ensures. It has labored in native assessments on Unity 5.5.0f3.
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here