трансформация. телепортация.Не плавно движется - PullRequest
0 голосов
/ 14 сентября 2018

Простой вопрос, но трудно найти ответ.

У меня есть механик крюка, где при нажатии клавиши B. крюк будет срабатывать в течение 5 секунд, затем должен вернуться.

Этот код работает нормально, просто когда код, выделенный для вызова объекта, не возвращается гладко, он телепортируется.Вот код, строка для конкретной проблемы в BOLD

public class Hook : MonoBehaviour
{ //Remember Couroutine is pretty much update()

public Transform Target;
private float Thrust; // Int for motion
public Rigidbody rb;
public float HookTravelTime; //Define float for seconds
bool isHookActive = false;  //Are we currently moving?
public float timeHookTraveling = 0f;




// Use this for initialization
void Start()
{
    Thrust = 75f;
    rb = GetComponent<Rigidbody>();
    float walkspeed = Thrust * Time.deltaTime;
}

void OnCollisionEnter(Collision col)
{
    if (col.gameObject.name == "mob")
    {
        Destroy(col.gameObject);
        print("Other code negated");
        rb.velocity = Vector3.zero;
        transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);
        isHookActive = false;
        timeHookTraveling = 0f;
    }
}


void ThrowHook()
{

    if (Input.GetKeyDown(KeyCode.B))
    {
        isHookActive = true;
        rb.AddForce(Vector3.forward * Thrust);
    }
    if (isHookActive )
        {
            if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
            {

            print("hehemeth");
            rb.velocity = Vector3.zero; //negate addforce from before
          **HERE**  transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);
            isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
            timeHookTraveling = 0f;//reset the travel time for your next hook activation
        }

            else//if you havent hit 5 keep increasing
            {
                timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
            }
        }
    }



// Update is called once per frame
void Update()
    {
    ThrowHook();
    }


}

Что я делаю не так?Должно работать как задумано, верно?

1 Ответ

0 голосов
/ 14 сентября 2018

Vector3.MoveTowards необходим для запуска на каждом кадре, поэтому я упомянул * Time.deltaTime в комментарии.

Через 5 с скорость rb становится равной нулю, а isHookActive становится ложной, и, таким образом, Vector3.MoveTowards не называется каждым кадром.

if (Input.GetKeyDown(KeyCode.B))
{
    isHookActive = true;
    rb.AddForce(Vector3.forward * Thrust);
}
if (isHookActive )
{
    if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
    {

        print("hehemeth");
        rb.velocity = Vector3.zero; //negate addforce from before
        isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
        timeHookTraveling = 0f;//reset the travel time for your next hook activation
    }

    else//if you havent hit 5 keep increasing
    {
        timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
    }
}

else if (!isHookActive && transform.position != Target.position)
{
    transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust * Time.deltaTime);
}

И еще лучшим способом является

if (Input.GetKeyDown(KeyCode.B))
{
    isHookActive = true;
    rb.AddForce(Vector3.forward * Thrust);
}

в FixedUpdate(), но не Update(), в то время как

if (isHookActive )
{... }

остается в Update().

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...