Unity C#与玩家对象关联的移动

huangapple go评论69阅读模式
英文:

Unity C# movement tied to player object

问题

我正在玩一个二维太空射击游戏。目前,我正在尝试从飞船前方发射一个抛射物(存在武器和弹夹对象,但该语句通常为真),并将其投射到太空中。问题是,它发射并向前移动,但x轴跟随我的飞船。所以当我移动时,子弹前进但左右移动。

我假设当设置位置时,我正在给它一个对飞船位置的引用,但我尝试将其更改为基于当前位置的向量的新实例,但它仍然跟随我的飞船。如何解除这两个对象的运动耦合?

public void Shoot()
{
    PlayerWeaponMagazine magazineScript = magazine.GetComponent<PlayerWeaponMagazine>();
    GameObject bullet = magazineScript.UseBullet();
    PlayerWeaponBullet bulletScript = bullet.GetComponent<PlayerWeaponBullet>();
    
    // 用于生成子弹的相关代码
    var vector3 = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y);
    
    // 假设这只是一个值,而不是一个引用
    bullet.transform.position = vector3;
    bullet.SetActive(true);
}

子弹移动的代码:

void Update()
{
    timePassed += Time.deltaTime;
    if (gameObject.activeSelf)
    {
        // 用于移动抛射物的相关代码
        gameObject.transform.position += (bulletSpeed * Vector3.up * Time.deltaTime);
        if (timePassed > bulletLifetime)
        {
            gameObject.SetActive(false);
            timePassed = 0f;
        }
    }
}

UseBullet() 只是一个池管理器(从注释中可以看出):

public GameObject UseBullet()
{
    GameObject bullet = pool[poolIndex];
    poolIndex++;
    if (poolIndex > capacity - 1)
    {
        poolIndex = 0;
    }
    return bullet;
}

为了清晰起见,添加以下内容:

void Start()
{
    pool = new GameObject[capacity];
    for (int i = 0; i < capacity; i++)
    {
        GameObject bullet = Instantiate(bulletPrefab);
        bullet.SetActive(false);
        //bullet.transform.SetParent(gameObject.transform);
        pool[i] = bullet;
    }
}

希望这可以帮助你解决子弹位置跟随飞船的问题。

英文:

I am toying around with a 2d space shooter. Currently I am trying to fire a projectile forward from the ship (there is a weapon and magazine object but the statement is generally true) and send it hurling into space on its trajectory. The issue is it fires and moves forward but the x axis follow my ship. So I get this strange effect of the bullet going forward but left or right as I move.

My assumption was that when setting the position I was giving it a reference to the ship's position but I tried changing it to a new instance of the vector based on the current position but it still follows my ship. How do I decouple the two objects movement?

public void Shoot()
{
    PlayerWeaponMagazine magazineScript = magazine.GetComponent&lt;PlayerWeaponMagazine&gt;();
    GameObject bullet = magazineScript.UseBullet();
    PlayerWeaponBullet bulletScript = bullet.GetComponent&lt;PlayerWeaponBullet&gt;();
    
    //Relevant Code for spawning the bullet
    var vector3 = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y);
    
    //Assuming this is just a value now and not a reference
    bullet.transform.position = vector3;
    bullet.SetActive(true);
}

Code for the bullet to move:

void Update()
{
    timePassed += Time.deltaTime;
    if (gameObject.activeSelf)
    {
        
        //Relevant code for moving the projectile.
        gameObject.transform.position += ((bulletSpeed * Vector3.up * Time.deltaTime));
        if (timePassed &gt; bulletLifetime)
        {
            gameObject.SetActive(false);
            timePassed = 0f;
        }
    }
}

The UseBullet() is just a pool manager (from comments)

public GameObject UseBullet()
{
    GameObject bullet = pool[poolIndex];
    poolIndex++;
    if (poolIndex &gt; capacity - 1)
    {
        poolIndex = 0;
    }
    return bullet;
}

Adding this for clarity on the answer

void Start()
{
    pool = new GameObject[capacity];
    for (int i = 0; i &lt; capacity; i++)
    {
        GameObject bullet = Instantiate(bulletPrefab);
        bullet.SetActive(false);
        //bullet.transform.SetParent(gameObject.transform);
        pool[i] = bullet;
    }
}

答案1

得分: 1

你没有展示这个调用的代码,这很可能是你的问题所在:

magazineScript.UseBullet();

根据你的描述,听起来你是将子弹生成为武器(或飞船)的子对象。作为子对象,它将继承(在数学术语中,相当于乘以)其父对象的变换。

你应该在飞船外生成(或更好地重复使用)子弹,因为一旦它们生成,它们将响应自己的物理规则,而不是飞船的物理规则。

英文:

You don't show the code for this call, which is where your issue most likely lies:

magazineScript.UseBullet();

Specifically, from your description, it sounds like you're spawning the bullet as a child of the weapon (or the ship). Being a child, it will inherit (in math terms, multiply) the transform of its parents into its own.

You should be spawning (or even better, reusing) bullets outside of your ship, because once they're spawned they respond to their own physics, not their ship's physics.

答案2

得分: 0

使用 "instantiate" 来创建你的子弹。

为你的子弹添加一个脚本,告诉它以某个方向和一定的速度移动。

然后在脚本中使用 "instantiate" 在一个起始位置实例化子弹预制体(如果是飞船或枪支,起始位置应该在枪管的末端),就像这样:

// 在位置 (0, 0, 0) 处以零旋转实例化。
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);

查看 Unity 文档以获取有关 "instantiate" 及其用法的更多信息。
https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

英文:

Use instantiate for your bullets.

give your bullet a scritp in what you tell it to move in a dicection and a certain speed.

next in the script shoot instantiate the bullet prefab at a starting position (in case of ship or gun, the start possition should be at the end of a gun barrel)
like this:

    // Instantiate at position (0, 0, 0) and zero rotation.
    Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);

see the unity documnetation for more info about insantiate and how to use it.
https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

huangapple
  • 本文由 发表于 2023年7月17日 22:45:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定