简体   繁体   English

Unity中的磁铁脚本问题

[英]Magnet script issues in Unity

I'm trying to make a magnet PowerUp in Unity. 我正在尝试在Unity中制作磁铁PowerUp。 I used this script, which I attached to the coin GameObject: 我使用了此脚本,并将其附加到硬币GameObject上:

public GameObject attractedTo; 

private float strengthOfAttraction = 3f;

void FixedUpdate ()
    {
    Vector3 direction = attractedTo.transform.position - transform.position;
    gameObject.GetComponent<Rigidbody>().AddForce (strengthOfAttraction * direction);
    }

I have two problems : 1. Only newly spawn coins are attracted to the player 2. For some reason, the coins move only in straight lines, so most of them goes past the player 我有两个问题:1.只有新生成的硬币被吸引到玩家手中2.由于某些原因,硬币只能沿直线移动,因此它们中的大多数会经过玩家

Anyone knows how this can be fixed? 有谁知道该如何解决? Any help will be appreciated! 任何帮助将不胜感激!

Major issue on your code. 代码上的主要问题。 You have to recreate your procedure logic on how you make the coins get closer to the Player. 您必须重新创建有关如何使硬币更接近玩家的过程逻辑。

First Major problem is this. 第一个主要问题是这个。

void FixedUpdate() So on a fix amount of time per frame. void FixedUpdate()因此,每帧固定的时间。 You are trying to Declare a Variable Vector3 : Vector3 direction = attractedTo.transform.position - transform.position; 您正在尝试声明一个变量Vector3: Vector3 direction = attractedTo.transform.position - transform.position;

So frame will recalculate and create a new pointer name direction then you try to find gameObject.GetComponent<Rigidbody>().AddForce (strengthOfAttraction * direction); 因此,框架将重新计算并创建一个新的指针名称direction然后尝试找到gameObject.GetComponent<Rigidbody>().AddForce (strengthOfAttraction * direction); your Own Component in your gameObject. 您在gameObject中的自己的组件。 Then Call Addforce. 然后致电Addforce。 AddForce. AddForce。 And you didn't even use time.deltatime. 而且您甚至都没有使用time.deltatime。

Addforce Will push the rigidbody depending on the direction you input. Addforce将根据您输入的方向推动刚体。 So it will go straight. 所以它会一直下去。 And you are doing this Every Fix amount of frame per sec. 并且您正在执行每秒每帧固定数量的帧。

First fix your procedure. 首先修复您的程序。 My recommendation is this. 我的建议是这个。

Use the Smooth Follow script. 使用平滑跟踪脚本。 Made by Unity3D get your Reference there. 由Unity3D制造,在那里可以找到您的参考。 It is also installed already in the script section in your Editor. 它还已经安装在编辑器的脚本部分中。 If you are having problem finding it, go to this link and check he script. 如果找不到问题,请转到此链接并检查脚本。

Smooth Follow Script 平滑跟随脚本

[ Update Answer ] [ 更新答案 ]

For the 2nd Problem that only new coins is getting attracted. 对于第二个问题,只有新硬币被吸引。

Check all of this situations. 检查所有这些情况。

  1. Your Player is newly spawned and your attractedTo variable is blank. 您的播放器是新生成的,您的attractedTo变量为空白。
  2. Non newly spawned coins should have referenced to the Player already, if not you have to tell all the coins that if you spawn the Player, they needed to be attracted to Player. 非新生成的硬币应该已经参考了玩家,否则,您必须告诉所有硬币,如果您生成了玩家,则它们需要被玩家吸引。 -> Assign attractedTo when Player Spawned. - >分配attractedTo当球员催生。

You can either Achieve that on Instantiate then find all GameObjects with TAG. 您可以通过实例化实现,然后找到所有带有TAG的GameObject。 Then getcomponent then assign attractedTo 然后getcomponent然后分配attractedTo

  1. Your non Spawned coins, doesn't have the Script Component. 您未生成的硬币没有脚本组件。
  2. Your non Spawned coins, is disabled. 您未生成的硬币已被禁用。

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

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