Applying direction into transform position
Hello! I and my team are new in Unity and I would ask a simple question. I'm developing an action game which involving music as the core of its gameplay, it still under prototype, but I'm facing serious problem regarding to core gameplay.
It contains a set of entities (game objects) that move into several directions; top, right, left, bottom and diagonal moves (e.g: top-left, etc etc).
The calculation of these entities position require special calculation and I able to produce it in Unity (these position involving tempo of a music), fyi this position is calculated in Update() function.
And I use following function to determine the position of transform of the entities:
private Vector3 _position;
private void DeterminePosition(float offset)
{
// In actual code, the _position is initialized under Start() method
// But for this simplification sake, I'll just put it here
_position = _position == null ? new Vector3(0, 0, 1f) : _position;
if (Direction == Direction.Up || Direction == Direction.RightUp || Direction == Direction.LeftUp)
{
_position.y = offset;
}
if (Direction == Direction.Down || Direction == Direction.RightDown || Direction == Direction.LeftDown)
{
_position.y = -offset;
}
if (Direction == Direction.Left || Direction == Direction.LeftDown || Direction == Direction.LeftUp)
{
_position.x = -offset;
}
if (Direction == Direction.Right || Direction == Direction.RightDown || Direction == Direction.RightUp)
{
_position.x = offset;
}
transform.position = _position;
}
The code works perfectly as intended, however the game need to be change. Instead of fixed direction (e.g: Up, Right, Left, Bottom, RightUp, DownLeft etc etc) we decide to use value of degree for the direction (0 to 360 degree).
And now I've no idea how to impement this. I've tried to use following codes, but it doesn't work:
_position = new Vector3(offset, offset, transform.position.z);
// Where the direction is between 0 .. 360
transform.position = Quaternion.Euler(0, direction, 0) * _position;
Can anybody else come up with solution? Thanks in advance!
Your answer
Follow this Question
Related Questions
Problem with aiming Script(Only Some Code Works) 0 Answers
How to properly make an object follow the edge of another object? 0 Answers
Gameobjects transform position minus range 1 Answer
is it possible for velocity vectors to be added on the players axis? 1 Answer
How to create Curve between two points using **Vector3.MoveTowards**? 0 Answers