Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么当物体向目标移动时,如果目标距离较远,物体移动得很快,但如果目标距离较近,物体移动得很快';他走慢了吗?_C#_Unity3d - Fatal编程技术网

C# 为什么当物体向目标移动时,如果目标距离较远,物体移动得很快,但如果目标距离较近,物体移动得很快';他走慢了吗?

C# 为什么当物体向目标移动时,如果目标距离较远,物体移动得很快,但如果目标距离较近,物体移动得很快';他走慢了吗?,c#,unity3d,C#,Unity3d,我希望它像在近景物体中一样移动得慢一些 小立方体正在从玩家的手移动到右边的大立方体。 右边的大立方体是目标 这是附加脚本的玩家根对象。投掷速度设置为0.5f 这是我调用ThrowObject方法的更新中的脚本: using UnityEngine; using System; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; [RequireComponent(typeof(A

我希望它像在近景物体中一样移动得慢一些

小立方体正在从玩家的手移动到右边的大立方体。 右边的大立方体是目标

这是附加脚本的玩家根对象。投掷速度设置为0.5f

这是我调用ThrowObject方法的更新中的脚本:

using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour
{
    public InteractableItem[] lookObj = null;
    public GameObject objToThrow;
    public Text text;
    public Text textMultiLine;
    public float weightDamping = 1.5f;
    public float maxDistance = 10f;
    public bool RightHandToTarget = true;
    public float throwSpeed;

    private List<InteractableItem> allDetectedItems;
    private Animator animator;
    private InteractableItem lastPrimaryTarget;
    private Quaternion savedRotation;
    private float lerpEndDistance = 0.1f;
    private float finalLookWeight = 0;
    private bool transitionToNextTarget = false;
    private InteractableItem target;
    private bool throwObj = false;

    void Start()
    {
        animator = GetComponent<Animator>();
        allDetectedItems = new List<InteractableItem>();
    }

    // Callback for calculating IK
    void OnAnimatorIK()
    {
        if (lookObj != null)
        {
            InteractableItem primaryTarget = null;
            float closestLookWeight = 0;

            // Here we find the target which is closest (by angle) to the players view line
            allDetectedItems.Clear();
            foreach (InteractableItem target in lookObj)
            {
                Vector3 lookAt = target.transform.position - transform.position;
                lookAt.y = 0f;

                // Filter out all objects that are too far away
                //if (lookAt.magnitude > maxDistance) continue;
                if (lookAt.magnitude > target.distance) continue;

                float dotProduct = Vector3.Dot(new Vector3(transform.forward.x, 0f, transform.forward.z).normalized, lookAt.normalized);
                float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
                if (lookWeight > 0.1f && lookWeight > closestLookWeight)
                {
                    closestLookWeight = lookWeight;
                    primaryTarget = target;
                    allDetectedItems.Add(target);
                }
            }

            if (primaryTarget != null)
            {
                if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
                {
                    // Here we start a new transition because the player looks already to a target but
                    // we have found another target the player should look at
                    transitionToNextTarget = true;
                }
            }

            // The player is in a neutral look position but has found a new target
            if ((primaryTarget != null) && !transitionToNextTarget)
            {
                lastPrimaryTarget = primaryTarget;
                //finalLookWeight = Mathf.Lerp(finalLookWeight, closestLookWeight, Time.deltaTime * weightDamping);
                finalLookWeight = Mathf.Lerp(finalLookWeight, 1f, Time.deltaTime * weightDamping);
                float bodyWeight = finalLookWeight * .75f;
                animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
                animator.SetLookAtPosition(primaryTarget.transform.position);

                if (RightHandToTarget)
                {
                    Vector3 relativePos = primaryTarget.transform.position - transform.position;
                    Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
                    animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
                    animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
                    animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 1f * closestLookWeight);
                    animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);

                    // -> new code block
                    if (finalLookWeight > 0.95f) // here you can play with a value between 0.95f -> 1.0f
                    {
                        // call your funtion to shoot something here
                        throwObj = true;
                        target = primaryTarget;
                    }
                }
            }

            // Let the player smoothly look away from the last target to the neutral look position
            if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
            {
                finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
                float bodyWeight = finalLookWeight * .75f;
                animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
                animator.SetLookAtPosition(lastPrimaryTarget.transform.position);

                if (RightHandToTarget)
                {
                    Vector3 relativePos = lastPrimaryTarget.transform.position - transform.position;
                    Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
                    animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
                    animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
                    animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 0.5f * closestLookWeight);
                    animator.SetIKPosition(AvatarIKGoal.RightHand, lastPrimaryTarget.transform.position);
                }

                if (finalLookWeight < lerpEndDistance)
                {
                    transitionToNextTarget = false;
                    finalLookWeight = 0f;
                    lastPrimaryTarget = null;
                }
            }

            // Show primary object found by the player
            if (primaryTarget != null) text.text = "Item found: " + primaryTarget.description;
            else text.text = "Item found: none";

            // Show all objects found by the player
            if (allDetectedItems.Count > 0)
            {
                string result = "";
                foreach (InteractableItem item in allDetectedItems)
                {
                    result += item.description + "\n";
                }
                textMultiLine.text = result;
            }
            else
            {
                textMultiLine.text = "No items found.";
            }
        }
    }

    private void ThrowObject()
    {
        objToThrow.transform.position = Vector3.Lerp(objToThrow.transform.position, target.transform.position, throwSpeed * Time.deltaTime);
    }

    private void Update()
    {
        if (throwObj == true)
        {
            ThrowObject();
        }
    }
}
使用UnityEngine;
使用制度;
使用系统集合;
使用UnityEngine.UI;
使用System.Collections.Generic;
[RequiredComponent(typeof(Animator))]
公共类控制:单一行为
{
公共可交互项[]lookObj=null;
公共游戏对象对象对象;
公共文本;
公共文本文本多行;
公众浮子重量阻尼=1.5f;
公共浮动最大距离=10f;
public bool RightHandToTarget=真;
公众漂浮物的速度;
列出所有检测到的项目;
私人动画师;
私有可交互项lastPrimaryTarget;
私有四元数存储库;
专用浮球距离=0.1f;
私人浮动最终加权=0;
private bool transitiononnexttarget=false;
私有可交互项目标;
private bool throwObj=假;
void Start()
{
animator=GetComponent();
allDetectedItems=新列表();
}
//用于计算IK的回调
void OnAnimatorIK()
{
if(lookObj!=null)
{
InteractiableItem primaryTarget=null;
浮球闭合器的重量=0;
//在这里,我们找到了距离玩家视线最近的目标(角度)
所有检测到的项。清除();
foreach(lookObj中的InteractiableItem目标)
{
Vector3注视=target.transform.position-transform.position;
瞧,y=0f;
//过滤掉所有太远的对象
//如果(lookAt.magnitude>maxDistance)继续;
如果(lookAt.magnity>target.distance)继续;
float dotProduct=Vector3.Dot(新的Vector3(transform.forward.x,0f,transform.forward.z).规范化,lookAt.normalized);
浮动容重=数学夹具(点积,0f,1f);
如果(lookWeight>0.1f&&lookWeight>closestLookWeight)
{
closestLookWeight=外观重量;
主要目标=目标;
所有检测项目。添加(目标);
}
}
if(primaryTarget!=null)
{
如果((lastPrimaryTarget!=null)&&(lastPrimaryTarget!=primaryTarget)&&(finalLookWeight>0f))
{
//在这里,我们开始一个新的过渡,因为玩家已经看到了一个目标,但是
//我们找到了另一个目标,玩家应该看看
TransitionNoxtTarget=true;
}
}
//玩家处于中立注视位置,但已找到新目标
if((primaryTarget!=null)&&!TransitionOnNext目标)
{
lastPrimaryTarget=primaryTarget;
//finalLookWeight=Mathf.Lerp(finalLookWeight,closestLookWeight,Time.deltaTime*weightsdamping);
finalLookWeight=Mathf.Lerp(finalLookWeight,1f,Time.deltaTime*权重阻尼);
浮体重量=最终重量*.75f;
动画师。设置容重(最终容重,体重,1f);
animator.SetLookAtPosition(primaryTarget.transform.position);
如果(右手目标)
{
Vector3 relativePos=primaryTarget.transform.position-transform.position;
四元数rotationtoTarget=四元数.LookRotation(relativePos,Vector3.up);
动画师。设定旋转权重(化身右侧,最终权重);
动画师。设定旋转(化身右侧,旋转到目标);
动画师。设置位置权重(头像右侧,最终重量*1f*闭合重量);
animator.SetIKPosition(AvatarIKGoal.rightnand,primaryTarget.transform.position);
//->新代码块
if(finalLookWeight>0.95f)//在这里,您可以使用介于0.95f->1.0f之间的值进行播放
{
//打电话给你的职能部门在这里拍摄一些东西
throwObj=真;
目标=主要目标;
}
}
}
//让玩家平稳地将视线从最后一个目标转移到中立注视位置
if((primaryTarget==null&&lastPrimaryTarget!=null)| | TransitionNext目标)
{
finalLookWeight=Mathf.Lerp(finalLookWeight,0f,Time.deltaTime*权重阻尼);
浮体重量=最终重量*.75f;
动画师。设置容重(最终容重,体重,1f);
SetLookAtPosition(lastPrimaryTarget.transform.position);
如果(右手目标)
{
Vector3 relativePos=lastPrimaryTarget.transform.position-transform.position;
四元数rotationtoTarget=四元数.LookRotation(relativePos,Vector3.up);
动画师。设定旋转权重(化身右侧,最终权重);
动画师。设定旋转(化身右侧,旋转到目标);
动画师。设置位置权重(头像右侧,最终重量*0.5f*闭合重量);
animator.SetIKPosition(AvatarIKGoal.rightnand,lastPrimaryTarget.transform.position);
}
如果(最终重量    1) [0,0] + ([10,10] - [0,0]) * 0.1 = [0,0] + [10,10]*0.1 = [0,0] + [1,1] = [1,1]
    2) [0,0] + ([10,10] - [0,0]) * 0.2 = [0,0] + [10,10]*0.2 = [0,0] + [2,2] = [2,2]
    3) [0,0] + ([10,10] - [0,0]) * 0.3 = [0,0] + [10,10]*0.3 = [0,0] + [3,3] = [3,3]
    1) [0,0] + ([500,500] - [0,0]) * 0.1 = [0,0] + [500,500]*0.1 = [0,0] + [50,50] = [50,50]
    2) [0,0] + ([500,500] - [0,0]) * 0.2 = [0,0] + [500,500]*0.2 = [0,0] + [100,100] = [100,100]
    3) [0,0] + ([500,500] - [0,0]) * 0.3 = [0,0] + [500,500]*0.3 = [0,0] + [150,150] = [150,150]
Vector3 dir; // direction of moving. This parameter will be given to the function Translate()
GameObject targetPosition; // Target position
float speed; // The speed of moving. Recommended to be set from inspector
private void ThrowObject () {
    dir = targetPosition - transform.position;
    transform.Traslate(dir.normalized*Time.deltaTime*speed);
}
private void Update () {
    ThrowObject();
}