简体   繁体   English

为什么敌人会在这个地方? 为什么旋转速度永远不会改变?

[英]Why the enemy is positioned at this place ? And why the rotation speed never change?

using UnityEngine;
using System.Collections;

public class WaypointsMover : MonoBehaviour {

    public float rotateSpeed = 2.0f;

    private bool rotating;

    void Update() {
        StartCoroutine(TurnTowards(-transform.forward));
    }

    IEnumerator TurnTowards(Vector3 lookAtTarget) {    

        if(rotating == false) {
            Quaternion newRotation = Quaternion.LookRotation(lookAtTarget - transform.position);
            newRotation.x = 0;
            newRotation.z = 0;

            for (float u = 0.0f; u <= 10.0f; u += Time.deltaTime * rotateSpeed) {
                rotating = true;
                transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, u);

                yield return null;
            }
            rotating = false;
        }
    }

    // Use this for initialization
    void Start () {

    }
}
  1. No matter if i change the value of rotateSpeed to 10.0f or to 20.0f the enemy will get to one side will walk some on place and then will turn and walk to the other side.无论我将 rotateSpeed 的值更改为 10.0f 还是 20.0f,敌人都会走到一侧,在原地行走,然后转身走到另一侧。 How can i make that the enemy will turn right away when getting to the side ?怎样才能让敌人一到一边就转身?

  2. Why the enemies positioned there on the building roof while in the scene i position them in another places at all ?为什么敌人定位在建筑物屋顶上,而在场景中我将它们定位在其他地方?

  3. How do i set waypoints ?如何设置航点? I can't find in the code way points.我在代码中找不到方法点。 I know that in the for loop it was in the original:我知道在 for 循环中它是原始的:

    for (float u = 0.0f; u <= 1.0f; u += Time.deltaTime * rotateSpeed) { for (float u = 0.0f; u <= 1.0f; u += Time.deltaTime * rotateSpeed) {

I changed it to 10.0f instead of 1.0f so now they are walking longer way on the roof.我把它改成 10.0f 而不是 1.0f,所以现在他们在屋顶上走得更远了。 But if i want to make that they will go between two spheres i have ?但是,如果我想让它们在我拥有的两个球体之间移动?

This is a screenshot of the scene the building on the right where they walk on it's roof when running the game.这是运行游戏时他们在屋顶上行走的场景的屏幕截图。 The two enemies(Guards) on the left on the right it's the Main Player.左边右边的两个敌人(守卫)是主要玩家。 The camera i set it to follow the first Guard.我将相机设置为跟随第一个守卫。

I also noticed there is not space between them so they collide each other or disturb each other to walk.我还注意到它们之间没有空间,因此它们相互碰撞或相互干扰行走。 Where or how can i set more space between them so each one will go on it's own path ?我在哪里或如何在它们之间设置更多空间,以便每个人都走自己的路?

场景

This is a short video clip i recorded showing how they are walking and where they are walking:这是我录制的一个简短视频片段,展示了他们如何走路以及走路的位置:

Video Clip视频剪辑

Have you tried to use rotateSpeed in the Quaternion.Slerp() function instead of var u?您是否尝试过在 Quaternion.Slerp() 函数中使用 rotateSpeed 而不是 var u? As Time.deltaTime has quite a low value (at first it should be smth around ~0.0145) it will lower the speed of rotation dramatically.由于 Time.deltaTime 具有相当低的值(起初它应该在 ~0.0145 左右),它会显着降低旋转速度。 And it won't feel different if the rotateSpeed is 10.0f or 20.0f as it will only be faster at the end (I haven't tried to execute it though)如果rotateSpeed 为10.0f 或20.0f 不会有什么不同,因为它最终只会更快(不过我还没有尝试执行它)

Also, Slerp transitions the rotation to look smooth, so if you want an instant transition, just set the rotation of the object to the value you need.此外,Slerp 将旋转过渡到看起来平滑,因此如果您想要即时过渡,只需将对象的旋转设置为您需要的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM