- Home /
How is a gameobject rotated when you set it's forward?
I'm doing my own Unity with OpenTK, and want to be able to rotate an object by setting its forward, right or up.
I already did the get of the vectors:
public Vector3 forward
{
get
{
return Vector3.TransformVector(Vector3.UnitZ, Matrix4.CreateRotationX(rotation.X) * Matrix4.CreateRotationY(rotation.Y) * Matrix4.CreateRotationZ(rotation.Z));
}
}
public Vector3 up
{
get
{
return Vector3.TransformVector(Vector3.UnitY, Matrix4.CreateRotationX(rotation.X) * Matrix4.CreateRotationY(rotation.Y) * Matrix4.CreateRotationZ(rotation.Z));
}
}
public Vector3 right
{
get
{
return Vector3.TransformVector(Vector3.UnitX, Matrix4.CreateRotationX(rotation.X) * Matrix4.CreateRotationY(rotation.Y) * Matrix4.CreateRotationZ(rotation.Z));
}
but now I need the sets.
Here is the whole component code. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenTK;
namespace DemoEngine
{
public class Transform : Component
{
private float GaR = (float)Math.PI / 180;
private float RaG = 180 / (float)Math.PI;
public Vector3 position;
public Vector3 rotation;
public Vector3 scale = Vector3.One;
private List<Transform> childList;
private Transform _parent;
public Transform parent
{
get
{
return _parent;
}
set
{
if (_parent != null)
{
_parent.childList.Remove(this);
}
_parent = value;
value.childList.Add(this);
}
}
public Vector3 forward
{
get
{
return Vector3.TransformVector(Vector3.UnitZ, Matrix4.CreateRotationX(rotation.X) * Matrix4.CreateRotationY(rotation.Y) * Matrix4.CreateRotationZ(rotation.Z));
}
}
public Vector3 up
{
get
{
return Vector3.TransformVector(Vector3.UnitY, Matrix4.CreateRotationX(rotation.X) * Matrix4.CreateRotationY(rotation.Y) * Matrix4.CreateRotationZ(rotation.Z));
}
}
public Vector3 right
{
get
{
return Vector3.TransformVector(Vector3.UnitX, Matrix4.CreateRotationX(rotation.X) * Matrix4.CreateRotationY(rotation.Y) * Matrix4.CreateRotationZ(rotation.Z));
}
}
public Transform()
{
childList = new List<Transform>();
}
public Matrix4 GenerateMatrix()
{
Matrix4 matrix = Matrix4.Scale(scale) *
Matrix4.CreateRotationX(rotation.X) *
Matrix4.CreateRotationY(rotation.Y) *
Matrix4.CreateRotationZ(rotation.Z) *
Matrix4.CreateTranslation(position);
if (_parent != null)
{
matrix *= _parent.GenerateMatrix();
}
return matrix;
}
public float xRotation
{
get
{
return rotation.X * RaG;
}
set
{
rotation.X = value * GaR;
}
}
public float yRotation
{
get
{
return rotation.Y * RaG;
}
set
{
rotation.Y = value * GaR;
}
}
public float zRotation
{
get
{
return rotation.Z * RaG;
}
set
{
rotation.Z = value * GaR;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Need help in setting a game-object's rotation to euler-degrees. 0 Answers
Shooting problem the bullets fly in diffrent direction then player looks? 2 Answers
Wrong rotation while moving 1 Answer
Is there such a thing as Unity Algebra? 0 Answers
Efficient way to keep a fixed rotation for sprite? 0 Answers