Grappling Hook没有在Unity中擒抱玩家

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

我正在尝试在Unity中编写C#中的抓钩。我们的想法是将一个球游戏对象扔到一个标有“Hookable”标签的游戏​​对象中,并将玩家带到抓球的地方。球可以投掷并成功识别碰撞,但不会带球员。代码似乎是正确的,控制台没有给出任何警告但是,我不明白发生了什么不工作。这是代码,对不起,如果这听起来很无趣:

抓钩(附加到玩家游戏对象):

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

public class GrapplingHook : MonoBehaviour
{
    public GameObject hook;
    public GameObject hookHolder;

    public float hookTravelSpeed;
    public float playerTravelSpeed;

    public static  bool fired;
    public bool hooked;

    public float maxDistance;
    private float currentDistance;

    void Update()
    {
    //firing the hook
        if(/*Input.GetMouseButtonDown(0)*/Input.GetKeyDown(KeyCode.H) && fired == false)
            fired = true;

        if (fired == true && hooked == false)
        {
            hook.transform.Translate(Vector3.forward * Time.deltaTime * hookTravelSpeed);
            currentDistance = Vector3.Distance(transform.position, hook.transform.position);

            if(currentDistance >= maxDistance)
                ReturnHook();
        }

        if(hooked == true)
        {
            transform.position = Vector3.MoveTowards(transform.position,
            hook.transform.position, Time.deltaTime * playerTravelSpeed);
            float distanceToHook = Vector3.Distance(transform.position, hook.transform.position);

            if(distanceToHook < 1)
                ReturnHook();
        }
    }

    void ReturnHook()
    {
        hook.transform.position = hookHolder.transform.position;
        fired = false;
        hooked = false;
    }

}

这是检查是否检测到抓钩的代码(附加到Hook和Hookable对象):

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

public class HookDetector : MonoBehaviour
{
    public GameObject player;

   void OnTriggerEnter(Collider other)
   {
        if(other.tag == "Hookable")
        {
        player.GetComponent<GrapplingHook>().hooked = true;
        Debug.Log("Tocou");
        }
   }
}
c# unity3d game-engine game-development
1个回答
0
投票

问题解决了。我已将脚本附加到错误的游戏对象上,解决方案是附加到玩家GameObject。

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