在统一3d中使用行星重力时,剑士剑如何指向目标并跟随它?

问题描述 投票:0回答:1

我想知道是否有人可以帮助我。我试图让一个敌对的剑客跟着我的玩家,将剑对准我,但我似乎无法理解为什么它不起作用而发出刺眼的光芒(没有武器指向代码也可以起作用)

    private void FixedUpdate()
    {
        Follow(target);
        rb.MovePosition(rb.position + transform.TransformDirection(moveDir) * walkSpeed * Time.deltaTime);
        AimTowards();
    }

    public void Follow(Transform target)
    {
        Vector3 targetVector = target.position - transform.position;
        float speed = (targetVector.magnitude) * 5;
        Vector3 directionVector = targetVector.normalized * speed;

        directionVector = Quaternion.Inverse(transform.rotation) * directionVector;
        moveDir = new Vector3(directionVector.x, directionVector.y, directionVector.z).normalized;
    }
    void AimTowards()
    {
        float angle = Mathf.Atan2(moveDir.y, moveDir.x) * Mathf.Rad2Deg;

        pivot.localRotation = Quaternion.AngleAxis(angle - 90, Vector3.down);
    }

ScreenShot

这里是它的视频https://vimeo.com/364530113

这里是没有瞄准https://vimeo.com/364530129的视频

这里是使用Lookat时的视频(它很奇怪)https://vimeo.com/364530148

unity3d 3d
1个回答
0
投票

Lookat()是此问题的完美解决方案。


给出以下情况是,sword具有pivotparent(它们能够独立移动)和target。参见示例:

Example

在数据透视表上附加一个非常简单的脚本,该脚本将确保数据透视表将看向目标。另外,请确保剑在+ Z轴

上前进

Positive Z


在支点上的脚本中编写以下内容:

//Target object
public Transform target;
//Pivot object (in this case the object the script is attached to)
public Transform pivot;

//Might want to use Update() instead
private void FixedUpdate()
{
    //Simply look at the target
    pivot.LookAt(target);
}

无论父对象旋转还是平移,剑都应指向起点。


也,在您的脚本中:

Vector3 targetVector = target.position - transform.position;
float speed = (targetVector.magnitude) * 5;
Vector3 directionVector = targetVector.normalized * speed;

可以简化为:

Vector3 targetVector = target.position - transform.position;
float speed = 5.0f;
Vector3 directionVector = targetVector * speed;

由于归一化会将矢量的大小更改为1,因此,将其乘以真实大小将使它永不归一化。正如@ Draco18s所说,它不能确保恒定的运动。

© www.soinside.com 2019 - 2024. All rights reserved.