Я хочу, чтобы мой игрок больше не вращался после начала игры.Я замораживаю вращение в сценарии и в ограничениях тоже, но игрок все равно вращается, когда движется вперед.Что я могу сделать ?(У меня есть fps и контроллер персонажей).У меня также есть холст с кнопками для управления влево, верно?Должен ли я поместить скрипт рибибоди или игрока в объект персонажа (я создал игровой объект игрока, который содержит персонажа и камеру)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public float playerSpeed = 1500;
public float directionalSpeed = 20;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
}
// Update is called once per frame
void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
float moveHorizontal = Input.GetAxis("Horizontal");
transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + moveHorizontal, -2.5f, 2.5f), gameObject.transform.position.y, gameObject.transform.position.z), directionalSpeed * Time.deltaTime);
#endif
GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
transform.Rotate(Vector3.right * GetComponent<Rigidbody>().velocity.z / 3);
//MOBILE CONTROLS
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10f));
if (Input.touchCount > 0)
{
transform.position = new Vector3(touch.x, transform.position.y, transform.position.z);
}
}
public void MoveLeft()
{
rb.velocity = new Vector2(-playerSpeed, rb.velocity.y);
}
public void MoveRight ()
{
rb.velocity = new Vector2(playerSpeed, rb.velocity.y);
}
public void StopMoving()
{
rb.velocity = new Vector2(0f, rb.velocity.y);
}
void DetectInput()
{
float x = Input.GetAxisRaw("Horizontal");
if (x > 0 )
{
MoveRight();
}
else if ( x < 0)
{
MoveLeft();
}
else
{
StopMoving();
}
}
}