Как сделать чтобы объект не вращался при изменении родителя?
Добавлено: 13 май 2022, 11:51
Alexey7777
Здравствуйте, есть персонаж сфера (с чарактер контроллером) и платформа с пустым родителем, который вращается по оси Y, когда персонаж запрыгивает на платформу его родителем становится родитель платформы, а сам персонаж поворачивается по оси У нa Y родителя. Как сделать, чтобы персонаж не поворачивался, а смотрел только в ту сторону, куда прыгает? Вычитание и добавление углов поворота на угол поворота родителя не помогает.
Код
Код
Скрытый текст:
public class PlayerController : MonoBehaviour
{
[Header("----------- Контроллеры UI -----------")]
[SerializeField] private Camera cam;
[SerializeField] private MobileController mobContrlRot;
[SerializeField] private MobileController mobContrlMove;
[SerializeField] private Button jump;
[Space(20)]
[Header("----------- Дополнительные механики -----------")]
[SerializeField] private bool defining_platforms = false;
private CharacterController player;
public static float Rotate_player { set { rotationY += value; } }
private float playerSpeed = 8f;
private float jumpSpeed = 12f;
private float rotationSpeed = 5f;
private float minimumVert = -30f;
private float maximumVert = 40f;
private float rotationX = 0;
private static float rotationY = 0;
private float horInput = 0;
private float vertInput = 0;
private float gravity = Physics.gravity.y;
private float terminalVelocity = -10f;
private float minimumFall = -1.5f;
private float verticalSpeed;
private void Awake()
{
___ player = GetComponent<CharacterController>();
___ rotationY = transform.localEulerAngles.y;
}
// Update is called once per frame
void Update()
{
___ if (!GameManager.gameManager.gamePause)
___ {
____ PlayerMove();
____ PlayerRotation();
____ PlayerJump();
___}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
___ if (defining_platforms) DefiningPlatforms(hit);
___ if (hit.gameObject.CompareTag("Obstacle")) hit.gameObject.GetComponent<Obstacle>().Hit();
}
public void PlayerMove()
{
#if (UNITY_EDITOR)
___ horInput = Input.GetAxis("Horizontal");
___ vertInput = Input.GetAxis("Vertical");
#else
___ horInput = mobContrlMove.inputHorizontal();
___ vertInput = mobContrlMove.inputVertical();
#endif
___ Vector3 move = new Vector3(horInput, 0, vertInput);
___ move *= playerSpeed;
___ move.y = verticalSpeed;
___ move *= Time.deltaTime;
___ move = Vector3.ClampMagnitude(move, playerSpeed);
___ move = transform.TransformDirection(move);
___ player.Move(move);
}
private void PlayerRotation()
{
#if (UNITY_EDITOR)
___ rotationY += Input.GetAxis("Mouse X");
___ rotationX -= Input.GetAxis("Mouse Y");
#else
___ rotationY += mobContrlRot.inputHorizontal();
___ rotationX -= mobContrlRot.inputVertical();
#endif
___ rotationX = Mathf.Clamp(rotationX, minimumVert, maximumVert);
___ transform.localEulerAngles = Vector3.up * rotationY * rotationSpeed;
___ cam.transform.localEulerAngles = Vector3.right * rotationX;
}
public void PlayerJump(bool button = false)
{
___ if (player.isGrounded)
___ {
___ ___ if (Input.GetKeyDown(KeyCode.Space) || button)
___ ___ {
___ ___ ___ verticalSpeed = jumpSpeed;
___ ___ ___ if (this.transform.parent != null)
___ ___ ___ {
___ ___ ___ ___ this.transform.parent = null;
___ ___ ___ }
___ ___ }
___ ___ else
___ ___ {
___ ___ ___ verticalSpeed = minimumFall;
___ ___ }
___ }
___ else
___ {
___ verticalSpeed += gravity * 5 * Time.deltaTime;
___ if (verticalSpeed < terminalVelocity) verticalSpeed = terminalVelocity;
___ if (this.transform.parent != null) this.transform.parent = null;
___ }
}
private void DefiningPlatforms(ControllerColliderHit hit)
{
___ if (!player.isGrounded && hit.gameObject.CompareTag("Platform") && player.bounds.min.y >= hit.collider.bounds.max.y && this.transform.parent == null)
___ {
___ ___ this.transform.parent = hit.transform.parent;
___ }
}
}
{
[Header("----------- Контроллеры UI -----------")]
[SerializeField] private Camera cam;
[SerializeField] private MobileController mobContrlRot;
[SerializeField] private MobileController mobContrlMove;
[SerializeField] private Button jump;
[Space(20)]
[Header("----------- Дополнительные механики -----------")]
[SerializeField] private bool defining_platforms = false;
private CharacterController player;
public static float Rotate_player { set { rotationY += value; } }
private float playerSpeed = 8f;
private float jumpSpeed = 12f;
private float rotationSpeed = 5f;
private float minimumVert = -30f;
private float maximumVert = 40f;
private float rotationX = 0;
private static float rotationY = 0;
private float horInput = 0;
private float vertInput = 0;
private float gravity = Physics.gravity.y;
private float terminalVelocity = -10f;
private float minimumFall = -1.5f;
private float verticalSpeed;
private void Awake()
{
___ player = GetComponent<CharacterController>();
___ rotationY = transform.localEulerAngles.y;
}
// Update is called once per frame
void Update()
{
___ if (!GameManager.gameManager.gamePause)
___ {
____ PlayerMove();
____ PlayerRotation();
____ PlayerJump();
___}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
___ if (defining_platforms) DefiningPlatforms(hit);
___ if (hit.gameObject.CompareTag("Obstacle")) hit.gameObject.GetComponent<Obstacle>().Hit();
}
public void PlayerMove()
{
#if (UNITY_EDITOR)
___ horInput = Input.GetAxis("Horizontal");
___ vertInput = Input.GetAxis("Vertical");
#else
___ horInput = mobContrlMove.inputHorizontal();
___ vertInput = mobContrlMove.inputVertical();
#endif
___ Vector3 move = new Vector3(horInput, 0, vertInput);
___ move *= playerSpeed;
___ move.y = verticalSpeed;
___ move *= Time.deltaTime;
___ move = Vector3.ClampMagnitude(move, playerSpeed);
___ move = transform.TransformDirection(move);
___ player.Move(move);
}
private void PlayerRotation()
{
#if (UNITY_EDITOR)
___ rotationY += Input.GetAxis("Mouse X");
___ rotationX -= Input.GetAxis("Mouse Y");
#else
___ rotationY += mobContrlRot.inputHorizontal();
___ rotationX -= mobContrlRot.inputVertical();
#endif
___ rotationX = Mathf.Clamp(rotationX, minimumVert, maximumVert);
___ transform.localEulerAngles = Vector3.up * rotationY * rotationSpeed;
___ cam.transform.localEulerAngles = Vector3.right * rotationX;
}
public void PlayerJump(bool button = false)
{
___ if (player.isGrounded)
___ {
___ ___ if (Input.GetKeyDown(KeyCode.Space) || button)
___ ___ {
___ ___ ___ verticalSpeed = jumpSpeed;
___ ___ ___ if (this.transform.parent != null)
___ ___ ___ {
___ ___ ___ ___ this.transform.parent = null;
___ ___ ___ }
___ ___ }
___ ___ else
___ ___ {
___ ___ ___ verticalSpeed = minimumFall;
___ ___ }
___ }
___ else
___ {
___ verticalSpeed += gravity * 5 * Time.deltaTime;
___ if (verticalSpeed < terminalVelocity) verticalSpeed = terminalVelocity;
___ if (this.transform.parent != null) this.transform.parent = null;
___ }
}
private void DefiningPlatforms(ControllerColliderHit hit)
{
___ if (!player.isGrounded && hit.gameObject.CompareTag("Platform") && player.bounds.min.y >= hit.collider.bounds.max.y && this.transform.parent == null)
___ {
___ ___ this.transform.parent = hit.transform.parent;
___ }
}
}