Как запретить игроку вращаться(Transform.Rotate)
В общем у меня есть игрок, который вращается с помощью Transform.Rotate. Я хочу чтобы при нажатии на кнопку, отключалась возможность вращать его. Я пытался сделать это с помощью FreezeRotation, но это не помогло
Для вращения игрока я использую следующую функцию, которая находится в скрипте AstroManeuver
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AstroManeuver : MonoBehaviour{
[SerializeField] AstroScript astroScr;
[Header("Maneuver variables")]
internal Vector2 mousePos;
internal float angle;
internal float astroRot;
[SerializeField] internal GameObject handRot;
[SerializeField] internal GameObject firePointRot;
internal bool right;
private void Start()
{
print("Astro Man");
}
private void Update()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate()
{
astroScr.astroManScr.ClampVelocity();
if (handRot.transform.eulerAngles.z < 90f || handRot.transform.eulerAngles.z > 270f)
{
transform.localScale = new Vector3(1, 1, 1);
handRot.transform.localScale = new Vector3(1, 1, 1);
firePointRot.transform.localRotation = Quaternion.Euler(0, 0, 90);
}
else if (handRot.transform.eulerAngles.z > 90f && handRot.transform.eulerAngles.z < 270f)
{
transform.localScale = new Vector3(-1, 1, 1);
handRot.transform.localScale = new Vector3(-1, -1, 1);
firePointRot.transform.localRotation = Quaternion.Euler(0, 180, 90);
}
}
internal void ClampVelocity()
{
float x = Mathf.Clamp(astroScr.rb.velocity.x, -astroScr.maxVelocity, astroScr.maxVelocity);
float y = Mathf.Clamp(astroScr.rb.velocity.y, -astroScr.maxVelocity, astroScr.maxVelocity);
astroScr.rb.velocity = new Vector2(x, y);
}
internal void ThrustForward(float amount)
{
Vector2 force = astroScr.astroShootScr.bulletRb.velocity.normalized * amount;
astroScr.rb.AddForce(force);
}
internal void Rotate(Transform t, float amount) //Функция которую я использую для вращения
{
t.Rotate(0, 0, amount);
}
}
Далее я вызываю ее в скрипте AstroInput
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AstroInput : MonoBehaviour{
[SerializeField] AstroScript astroScr;
[Header("Input variables")]
internal Vector2 dir;
internal Vector2 mousePos;
private void Start()
{
print("Astro Input");
}
private void Update()
{
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
astroScr.astroManScr.Rotate(transform, dir.x * -astroScr.rotationSpeed);
if (Input.GetButtonDown("Fire1") && (astroScr.canShoot == true))
{
astroScr.astroShootScr.Shoot();
astroScr.astroManScr.ThrustForward(-40f);
}
}
}
Мои попытки остановить вращение в скрипте AstroAttract
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AstroAttract : MonoBehaviour{
[SerializeField] AstroScript astroScr;
private bool isTouchingObstacle;
internal bool cling;
[SerializeField] private LayerMask whatIsObstacle;
private Vector3 posCur;
private Quaternion rotCur;
[SerializeField] internal GameObject arrow;
private void Start()
{
print("Astro Attrac");
astroScr.rb.constraints = RigidbodyConstraints2D.FreezeRotation;
}
private void Update()
{
RaycastHit2D hit = new RaycastHit2D();
hit = Physics2D.Raycast(transform.position, transform.TransformDirection(-Vector3.up), .7f, whatIsObstacle);
if (hit.collider.tag == "Obstacle")
{
isTouchingObstacle = true;
}
else
{
isTouchingObstacle = false;
}
print(transform);
rotCur = Quaternion.FromToRotation(Vector3.up, hit.normal);
posCur = new Vector3(transform.position.x, hit.point.y, transform.position.z);
if (isTouchingObstacle && (Input.GetButtonDown("Fire2")))
{
astroScr.rb.velocity = Vector2.zero;
astroScr.canShoot = false;
astroScr.mouseRotationActive = false;
cling = true;
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, 5f);
astroScr.rb.AddForce(posCur * 20f);
astroScr.rb.drag = 5f;
astroScr.rb.constraints = RigidbodyConstraints2D.FreezeRotation; //Вот эта строчка должна по-хорошему останавливать вращение
arrow.SetActive(true);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(transform.position, transform.TransformDirection(0f, -.7f, 0f));
}
}
Порядок вызова скриптов следующий:
- AstroManeuver
- AstroInput
- AstroAttract
Ответы (1 шт):
Автор решения: Any 3Develop
→ Ссылка
Попробуйте rb.angularVelocity - если установить Vector3.Zero вращение будет остановлено, документация.