【问题标题】:Projectiles not moving, and not starting from the player (Unity 2D / C#)弹丸不移动,也不从玩家开始(Unity 2D / C#)
【发布时间】:2021-11-03 21:09:19
【问题描述】:

信息
所以我正在尝试制作一个自上而下的无尽竞技场战斗游戏,你的盾牌也可以作为你的进攻手段。盾牌应该在玩家前方生成,并在玩家移动和旋转时保持在前方(同时按住 mouse1(它会这样做))。松开 mouse1 后,盾牌应该像弹丸一样向前发射。

问题
但是,当松开 mouse1 时,盾牌会向前移动一点点然后冻结,但无论玩家在哪里,他们都会像玩家在 0、0 处移动一样。

(我知道这有点令人困惑,所以这是一张照片:) @ 987654321@
(三角形是播放器)

代码
这是我的播放器控制器脚本:

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

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D rb;

    public GameObject Shield;

    public float moveSpeed = 4.3f;
    public float sheildSpeed = 2f;
    
    Vector2 movement;

    bool isDead = false;

    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        Vector3 mouseScreen = Input.mousePosition;
        Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);

        transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90); 

        if (Input.GetMouseButtonDown(0)) {
            Shield = Instantiate(Shield, transform.position + transform.forward + transform.up, transform.rotation);
            Shield.transform.parent = transform;
        }

        if (Input.GetMouseButtonUp(0)) {
            Shield.transform.parent = null;
        } 
    }

    void FixedUpdate() {
        if (!isDead) {
            rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
        }
    }
}

这是sheild脚本:

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

public class ShieldController : MonoBehaviour
{
    public float sheildSpeed = 2f;

    void FixedUpdate()
    {
        this.GetComponent<Rigidbody2D>().MovePosition(this.transform.up * sheildSpeed);       
    }
}

注意
我希望已经为您提供了足够的信息来帮助我,但是如果您需要/想要更多详细信息,请留下 aaa 评论,我希望能回复您所需的任何信息:)

【问题讨论】:

    标签: c# unity3d rotation projectile topdown


    【解决方案1】:

    嘿,我在自己的项目中对此进行了测试并成功了

    改变这个

    void FixedUpdate()
    {
        this.GetComponent<Rigidbody2D>().MovePosition(this.transform.up * sheildSpeed);       
    }
    

    进入这个

    public void LaunchForward(float speed)
    {
        this.GetComponent<Rigidbody2D>().velocity = transform.up * speed;
    }   
    

    我还稍微编辑了产卵

    public GameObject Shield;
    public GameObject ShieldInstance;
    
    if (Input.GetMouseButtonDown(0))
        {
            if (ShieldInstance != null) { return; }
            ShieldInstance = Instantiate(Shield, transform.position + transform.forward + transform.up, transform.rotation);
            ShieldInstance.transform.parent = transform;
        }
    
    if (Input.GetMouseButtonUp(0))
        {
            ShieldInstance.transform.parent = null;
            ShieldInstance.GetComponent<ShieldController>().LaunchForward(5f);
            Destroy(ShieldInstance, 3f);
        }
    

    但主要问题是防护罩本身的物理特性,代码与 2 个函数发生冲突,这导致了我认为的奇怪行为

    编辑:只需要设置一次速度,因此这里适合使用专用方法(credit derHugo)。 虽然您可以在玩家类中使用此方法,但如果您想在游戏中拥有更复杂的行为,则将其放在自己的类中会更好

    【讨论】:

    • FixedUpdate 中设置速度是完全没有必要的 .. 这样做就足够了 once ... 而不是if (this.transform.parent != null) { return; } 我宁愿通过释放按钮时调用的专用方法
    • 您是对的,我将编辑上面的帖子以附加这些更改
    • 非常感谢,现在完美运行! :D
    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2016-06-24
    相关资源
    最近更新 更多