Question by
The-Evster · Jun 12, 2017 at 09:26 PM ·
vector3functionsefficient
Need help making my code more efficient
I have a function that changes the rotation of the character based on the player input. It works but it looks ugly and there must be a more efficient way of writing it. targetRot is a Vector3 that the character rotates to. upRight, upLeft, etc. are also predetermined Vector3s
void PlayerRotationFunction(float _movX, float _movZ, GameObject _activePlayer)
{
if (_movX >= 7 && _movZ >= 7)
{
targetRot = upRight;
}
else if (_movX <= -7 && _movZ >= 7)
{
targetRot = upLeft;
}
else if (_movX >= 7 && _movZ <= -7)
{
targetRot = downRight;
}
else if (_movX <= -7 && _movZ <= -7)
{
targetRot = downLeft;
}
else if (_movX >= 7)
{
targetRot = right;
}
else if (_movX <= -7)
{
targetRot = left;
}
else if (_movZ >= 7)
{
targetRot = up;
}
else if (_movZ <= -7)
{
targetRot = down;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612124038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Vector3' Why is this? 1 Answer
Vector3 not an identifier? 1 Answer
Inspactor Roation to Vector3 0 Answers
How do I get all of my code in start function 1 Answer
An object reference is required to access non-static member/ Referencing Function 1 Answer