- Home /
Solved it myself
Local translation error
Hi. As a quick project I've decided to try to make something akin to Wing War, a basic multiplayer flight game.
To achieve this, I've created a "play field" object, where all of the planes are local objects. I move them around the play area using transform.translate. The play field moves through the game world and rotates.
Now, even though the planes are set to translate in the local X and Y axis, they end up moving in the Z-plane.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DuelManager : MonoBehaviour
{
public GameObject Camera;
public float CameraSpeed;
public Transform LookAtPosition;
public float ForwardSpeed;
public float XSpeed;
public float YSpeed;
public AnimationCurve CenterCurve;
public float XBounds;
public float YBounds;
public float Timer;
public float TurnDuration;
public float AverageX;
public float AverageY;
public int CurrentAttacker;
[Serializable]
public struct Players
{
public string PlayerName;
public Color PlayerColor;
public GameObject Plane;
public Input Horizontal;
public Input Vertical;
public Input Trigger;
public float XCoordinate;
public float YCoordinate;
}
[SerializeField]
public Players[] players;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
AverageX = 0;
AverageY = 0;
transform.Translate(transform.forward * Time.deltaTime* ForwardSpeed,Space.World);
Camera.transform.localPosition = Vector3.Lerp(Camera.transform.localPosition, new Vector3(players[CurrentAttacker].Plane.transform.localPosition.x, players[CurrentAttacker].Plane.transform.localPosition.y+10, -10), Time.time*CameraSpeed);
if (players[1].Plane.transform.localPosition.x >= 0)
{
players[1].Plane.transform.Translate(transform.right * Time.deltaTime* -CenterCurve.Evaluate(players[1].Plane.transform.localPosition.x), Space.Self);
}
else
{
players[1].Plane.transform.Translate(transform.right * Time.deltaTime * CenterCurve.Evaluate(-players[1].Plane.transform.localPosition.x), Space.Self);
}
players[1].Plane.transform.Translate(transform.right * Time.deltaTime * Input.GetAxis("Horizontal") * XSpeed, Space.Self);
players[1].Plane.transform.Translate(transform.up * Time.deltaTime * -Input.GetAxis("Vertical") * YSpeed, Space.Self);
players[1].Plane.transform.localPosition = new Vector3(players[1].Plane.transform.localPosition.x, players[1].Plane.transform.localPosition.y, 50);
for (int i = 0; i < players.Length; i++)
{
if (i == CurrentAttacker)
{
}
else
{
AverageX += players[i].Plane.transform.localPosition.x;
AverageY += players[i].Plane.transform.localPosition.y;
}
}
AverageX = AverageX / players.Length-1;
AverageY = AverageY / players.Length-1;
LookAtPosition.localPosition = new Vector3(AverageX, AverageY, 50);
Camera.transform.LookAt(LookAtPosition.position);
transform.Rotate(0, AverageX*0.01f, 0, Space.World);
}
}
In the above code, I force the plane to its correct Z-position on every frame, which initially seems to fix the problem. However, once the play field approaches a rotation of 90 degrees, movement becomes sluggish and the aircraft centering feature goes berserk. I can't figure out why this is as the plane continues to have a rotation of 0,0,0.
For reference, here's a video of what happens:: Video
Any ideas?
Follow this Question
Related Questions
Invisible barrier transform.Translate error 1 Answer
moving object on x axis without affecting z axis 1 Answer
Make an object that moves relative to the touch more natural 0 Answers
Block Game Object interaction with another Game Object with kinematic active 0 Answers
Move two objects at same time as they are one single object 0 Answers