首頁>Game>source

最近,我為2D遊戲實現了射击系統,但是该系統存在問题.問题在於,当玩家面對左侧時,子弹仍在向右射击.要解決此問题,我需要訪問 Vector2 名為 targetVelocity 檢查播放器是否正確. targetVelocity另一个指令碼中定義,稱為 PhysicsObject 专門用於播放器物理,並且播放器指令碼从中繼承(變數為 protected 但我將其更改為 public ).我只是不知道该如何在我的 Bullet中引用 指令碼,而我不想將播放器指令碼添加到專案符號中.另外,我已经阅讀了与之相似的過去的問题和答案,但是由於情况不同,我還没有找到答案.預先感谢。

This is my Bullet 指令碼,我想以某種方式引用 targetVelocity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
    [SerializeField] float speed;
    [SerializeField] float lifeTime;
    public GameObject destroyEffect;
    
    private void Start()
    {
        Invoke("DestroyBullet", lifeTime);
    }
    private void Update()
    {
        transform.Translate(Vector2.right * speed * Time.deltaTime);
    }
    void DestroyBullet()
    {
        Instantiate(destroyEffect, transform.position, Quaternion.identity);
        Destroy(gameObject);
    }
}

Here is the weapon script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
    public GameObject bullet;
    public Transform firePoint;
    private float timeBtwShots;
    public float startTimeBtwShots;
    private void Update()
    {
        if (timeBtwShots <= 0)
        {
            if (Input.GetButton("Fire1"))
            {
                Instantiate(bullet, firePoint.position, transform.rotation);
                timeBtwShots = startTimeBtwShots;
            }
        }
        else
        {
            timeBtwShots -= Time.deltaTime;
        }
    }
}

This is the player script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewPlayer : PhysicsObject
{
    [Header("Attributes")]
    [SerializeField] private float jumpPower = 10;
    [SerializeField] private float maxSpeed = 1;
    //Singleton instantation
    private static NewPlayer instance;
    public static NewPlayer Instance
    {
        get
        {
            if (instance == null) instance = GameObject.FindObjectOfType<NewPlayer>();
            return instance;
        }
    }
    // Update is called once per frame
    void Update()
    {
        targetVelocity = new Vector2(Input.GetAxis("Horizontal") * maxSpeed, 0);
        //If the player presses "Jump" and we're grounded, set the velocity to a jump power value
        if (Input.GetButtonDown("Jump") && grounded)
        {
            velocity.y = jumpPower;
        }
        //Flip the player's localScale.x if the move speed is greater than .01 or less than -.01
        if (targetVelocity.x < -.01)
        {
            transform.localScale = new Vector2(-1, 1);
        }
        else if (targetVelocity.x > .01)
        {
            transform.localScale = new Vector2(1, 1);
        }
}
}
最新回復
  • 5月前
    1 #

    對專案符號的簡單解決方法是抓住開始建立專案符號的方向,然後以這種方式飞行.可能会有更優雅的解決方案,並且對targetVelocity的引用似乎很奇怪,但這就是您要的。

    private Vector2 fireDirection;
    private void Start()
    {
        fireDirection = new Vector2(Math.Sign(NewPlayer.Instance.targetVelocity.x), 0);
        Invoke("DestroyBullet", 2);
    }
    private void Update()
    {
        transform.Translate(fireDirection * speed * Time.deltaTime, Space.World);
    }
    

  • unity:結合钳位大小和一些爆炸力時遇到問题
  • unity:如何正確停止滑動