Как заставить мою ручку вращаться после подпрыгивания при длительном нажатии кнопки? - PullRequest
4 голосов
/ 05 августа 2020

В основном я хочу сделать подпрыгивающую палку и управлять поворотом с помощью кнопки влево-вправо. Я столкнулся с проблемой, что поворот не так хорош, как я ожидал, потому что он не будет следовать за моей кнопкой, как будто на него что-то влияет после подпрыгивания ,

I'm using 2d physics material with friction = 1 and Bounciness = 0.9797 for perfect bouncing also attached to rigidbody2d.

I don't know, should I attach it on collider?

here my Player control Script:

public Rigidbody2D r2d;

public float vertical;
public float horizontal;
public Joystick joystick;

private void Start() {
    r2d = gameObject.GetComponent < Rigidbody2D > ();
    joystick = FindObjectOfType < Joystick > ();
}

private void Update() {
    Movement();
}

public void Movement() {
    r2d.velocity = r2d.velocity.normalized * 7f;

    //horizontal = joystick.Horizontal;
    //vertical = joystick.Vertical;

    //if (horizontal == 0 && vertical == 0)
    //{
    //    Vector3 curRot = transform.localEulerAngles;
    //    Vector3 homeRot = transform.localEulerAngles;

    //    transform.localEulerAngles = Vector3.Slerp(curRot, homeRot, Time.deltaTime * 2);
    //}
    //else
    //{
    //    transform.localEulerAngles = new Vector3(0f, 0f, Mathf.Atan2(horizontal, vertical) * -180 / Mathf.PI);
    //}
}

public Vector3 target;
public float rotationSpeed = 10f;
public float offset = 5;

public void turnLeft() {
    Vector3 dir = target - transform.position;
    float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    Quaternion rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
    transform.rotation = Quaternion.Slerp(transform.rotation, -rotation, rotationSpeed * Time.deltaTime);
}

public void turnRight() {
    Vector3 dir = target - transform.position;
    float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    Quaternion rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
}

1 Ответ

2 голосов
/ 05 августа 2020

Всякий раз, когда задействован Rigidbody / Rigidbody2D, вы не хотите манипулировать чем-либо через компонент .transform!

Это нарушает физику, обнаружение столкновений и приводит к странным движениям в основном трансформация "борьба" против физики за приоритет.

То, что вы, скорее всего, хотите сделать, это, например, настроить Rigidbody2D.angularVelocity

public void turnLeft()
{
    // Note that Time.deltaTime only makes sense if this is actually called every frame!
    r2d.angularVelocity -= rotationSpeed * Time.deltaTime;
}

public void turnRight()
{
    r2d.angularVelocity += rotationSpeed * Time.deltaTime;
}
...