子弹没有固定速度加力?如何在 2D 中正确地从左右两侧独立拍摄?
Bullets don't have force added at a fixed speed? How to properly shoot independently form left and right sides in 2D?
我是编码新手,大约有一年的经验。我不明白为什么我的脚本不会为我以固定速度发射的子弹增加力量。这似乎取决于玩家移动的速度,但我对速度和移动速度使用了 2 个不同的变量,所以我不知道为什么会这样。我将如何解决这个问题?我也试图做到这一点,以便当角色面向那个方向时,我可以从左右两侧独立拍摄,但它似乎不起作用。当我尝试修复它时,它要么抛出错误,要么子弹在实例化时停留在原位,要么子弹总是向右移动。如果我做错了,正确的做法是什么?
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public GameObject Bullet;
public float sec = 2f;
public float speed = 80;
public GameObject Enemy;
public Transform spawnPoint;
public Rigidbody2D bulletRB;
private float timeBtwShots;
public float startTimeBtwShots;
public AudioSource Gunshot;
public Animator animator;
private float vert;
private float horiz;
// Start is called before the first frame update
void Start()
{
if (gameObject.tag == "Bullet")
this.gameObject.SetActive(true);
StartCoroutine(LateCall());
IEnumerator LateCall()
{
yield return new WaitForSeconds(sec);
this.gameObject.SetActive(false);
}
}
void OnCollisionEnter2D(Collision2D Other)
{
Destroy(this.gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
this.gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
IEnumerator ShootingAnim()
{
timeBtwShots = startTimeBtwShots;
Gunshot.Play();
animator.SetBool("IsShooting", true);
yield return new WaitForSeconds(0.2f);
animator.SetBool("IsShooting", false);
}
void Update()
{
horiz = Input.GetAxis("Horizontal");
vert = Input.GetAxis("Vertical");
if (Input.GetMouseButtonDown(0))
{
Instantiate(bulletRB, spawnPoint.position, Quaternion.identity);
}
}
void FixedUpdate()
{
if (timeBtwShots <= 0)
{
bulletRB.AddForce(transform.TransformDirection(new Vector2(horiz, vert)).normalized * speed);
StartCoroutine(ShootingAnim());
}
else
{
timeBtwShots -= Time.deltaTime;
}
if (gameObject.tag == "Bullet")
{
Destroy(this.gameObject, 1f);
}
}
}
不是使用输入来确定子弹的方向,而是存储玩家正在看的方向。添加一个名为“currentDirection”的新 Vector2,并将其默认为 Vector2.right 或其他名称。当你计算玩家的移动时,这样做:
if (!Mathf.Approximately(horiz, 0) || !Mathf.Approximately(vert, 0))
currentDirection = new Vector2(horiz, vert).normalized;
您会注意到第一行检查任何一行是否不为零。我这样做是因为如果两个输入均为 0,则方向也为零。我们只想保存玩家输入的最后一个方向,而不是缺少方向。
接下来,子弹也应该有一个方向变量。生成子弹时,将子弹的方向设置为 currentDirection。您只想在生成子弹时执行此操作。如果你每帧都将子弹的方向设置为玩家当前的输入,那么它会根据玩家的输入不断改变方向。
然后让子弹朝它指定的方向移动。通常人们只会改变它的旋转以匹配方向并告诉它向前移动,但这可能取决于你希望你的艺术如何寻找子弹。
同样非常重要但无关紧要:您需要将所有刚体代码放在 FixedUpdate 中。物理只在固定更新期间发生,所以在更新之外做任何与物理相关的事情都可能导致问题。但是,当您这样做时,请在更新中保留您的输入检查。由于 FixedUpdates 不会在每一帧发生,如果将它们放在 FixedUpdate 中,输入可能会丢失。因此,添加一些输入变量并在更新中执行
jumpInput = Input.GetButtonDown("Jump");
horiz = Input.GetAxis("Horizontal");
vert = Input.GetAxis("Vertical");
然后在 FixedUpdate 中做你的物理:
if (jumpInput && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
终于想通了。我做了一个完整的重写,因为我使代码过于复杂。
这是最终的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
public float jumpTime = 1f;
public Rigidbody2D _rigidbody;
private bool facingRight;
public float sec = 2f;
public Vector2 bulletPos;
public GameObject bulletToRight, bulletToLeft;
public Transform spawnPoint;
public Rigidbody2D bulletRB;
public AudioSource Gunshot;
float horizontalMove = 0f;
public float fireRate = 0.5f;
float nextFire = 0f;
// public AudioSource Jump;
public Animator animator;
void HandleMovement(float horizontal)
{
transform.position += new Vector3(horizontal, 0, 0) * Time.deltaTime * MovementSpeed;
animator.SetFloat("velX", (horizontal));
}
void Flip(float horizontal)
{
if (horizontal < 0 && !facingRight || horizontal > 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
transform.position += new Vector3(horizontal, 0, 0) * Time.deltaTime * MovementSpeed;
horizontalMove = Input.GetAxisRaw("Horizontal") * MovementSpeed;
animator.SetFloat("velX", (horizontalMove));
}
}
IEnumerator ShootingAnim()
{
animator.SetBool("IsShooting", true);
yield return new WaitForSeconds(0.2f);
animator.SetBool("IsShooting", false);
}
void Update()
{
animator.SetFloat("velX", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
animator.SetBool("IsJumping", true);
// Jump.Play();
}
if (Mathf.Abs(_rigidbody.velocity.y) == 0f)
{
animator.SetBool("IsJumping", false);
}
if ((_rigidbody.velocity.x) >= 0.01f || (_rigidbody.velocity.x <= -0.01f))
{
animator.SetBool("IsRunning", true);
}
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
StartCoroutine(ShootingAnim());
nextFire = Time.time + fireRate;
Fire();
}
float horizontal = Input.GetAxis("Horizontal");
HandleMovement(horizontal);
Flip(horizontal);
}
void Fire()
{
bulletPos = spawnPoint.position;
if (!facingRight)
{
bulletPos += new Vector2(+0.02f, 0f);
Instantiate(bulletToRight, bulletPos, Quaternion.identity);
Gunshot.Play();
} else
{
bulletPos += new Vector2(-0.02f, 0f);
Instantiate(bulletToLeft, bulletPos, Quaternion.identity);
Gunshot.Play();
}
}
}
这是 BulletScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public GameObject Bullet;
public Transform spawnPoint;
public Rigidbody2D bulletRB;
public float velX = 5f;
float velY = 0f;
// Start is called before the first frame update
void Start()
{
bulletRB = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D Other)
{
Destroy(this.gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
this.gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
private void Update()
{
bulletRB.velocity = new Vector2(velX, velY);
Destroy(gameObject, 3f);
}
}
我是编码新手,大约有一年的经验。我不明白为什么我的脚本不会为我以固定速度发射的子弹增加力量。这似乎取决于玩家移动的速度,但我对速度和移动速度使用了 2 个不同的变量,所以我不知道为什么会这样。我将如何解决这个问题?我也试图做到这一点,以便当角色面向那个方向时,我可以从左右两侧独立拍摄,但它似乎不起作用。当我尝试修复它时,它要么抛出错误,要么子弹在实例化时停留在原位,要么子弹总是向右移动。如果我做错了,正确的做法是什么?
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public GameObject Bullet;
public float sec = 2f;
public float speed = 80;
public GameObject Enemy;
public Transform spawnPoint;
public Rigidbody2D bulletRB;
private float timeBtwShots;
public float startTimeBtwShots;
public AudioSource Gunshot;
public Animator animator;
private float vert;
private float horiz;
// Start is called before the first frame update
void Start()
{
if (gameObject.tag == "Bullet")
this.gameObject.SetActive(true);
StartCoroutine(LateCall());
IEnumerator LateCall()
{
yield return new WaitForSeconds(sec);
this.gameObject.SetActive(false);
}
}
void OnCollisionEnter2D(Collision2D Other)
{
Destroy(this.gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
this.gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
IEnumerator ShootingAnim()
{
timeBtwShots = startTimeBtwShots;
Gunshot.Play();
animator.SetBool("IsShooting", true);
yield return new WaitForSeconds(0.2f);
animator.SetBool("IsShooting", false);
}
void Update()
{
horiz = Input.GetAxis("Horizontal");
vert = Input.GetAxis("Vertical");
if (Input.GetMouseButtonDown(0))
{
Instantiate(bulletRB, spawnPoint.position, Quaternion.identity);
}
}
void FixedUpdate()
{
if (timeBtwShots <= 0)
{
bulletRB.AddForce(transform.TransformDirection(new Vector2(horiz, vert)).normalized * speed);
StartCoroutine(ShootingAnim());
}
else
{
timeBtwShots -= Time.deltaTime;
}
if (gameObject.tag == "Bullet")
{
Destroy(this.gameObject, 1f);
}
}
}
不是使用输入来确定子弹的方向,而是存储玩家正在看的方向。添加一个名为“currentDirection”的新 Vector2,并将其默认为 Vector2.right 或其他名称。当你计算玩家的移动时,这样做:
if (!Mathf.Approximately(horiz, 0) || !Mathf.Approximately(vert, 0))
currentDirection = new Vector2(horiz, vert).normalized;
您会注意到第一行检查任何一行是否不为零。我这样做是因为如果两个输入均为 0,则方向也为零。我们只想保存玩家输入的最后一个方向,而不是缺少方向。
接下来,子弹也应该有一个方向变量。生成子弹时,将子弹的方向设置为 currentDirection。您只想在生成子弹时执行此操作。如果你每帧都将子弹的方向设置为玩家当前的输入,那么它会根据玩家的输入不断改变方向。
然后让子弹朝它指定的方向移动。通常人们只会改变它的旋转以匹配方向并告诉它向前移动,但这可能取决于你希望你的艺术如何寻找子弹。
同样非常重要但无关紧要:您需要将所有刚体代码放在 FixedUpdate 中。物理只在固定更新期间发生,所以在更新之外做任何与物理相关的事情都可能导致问题。但是,当您这样做时,请在更新中保留您的输入检查。由于 FixedUpdates 不会在每一帧发生,如果将它们放在 FixedUpdate 中,输入可能会丢失。因此,添加一些输入变量并在更新中执行
jumpInput = Input.GetButtonDown("Jump");
horiz = Input.GetAxis("Horizontal");
vert = Input.GetAxis("Vertical");
然后在 FixedUpdate 中做你的物理:
if (jumpInput && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
终于想通了。我做了一个完整的重写,因为我使代码过于复杂。 这是最终的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
public float jumpTime = 1f;
public Rigidbody2D _rigidbody;
private bool facingRight;
public float sec = 2f;
public Vector2 bulletPos;
public GameObject bulletToRight, bulletToLeft;
public Transform spawnPoint;
public Rigidbody2D bulletRB;
public AudioSource Gunshot;
float horizontalMove = 0f;
public float fireRate = 0.5f;
float nextFire = 0f;
// public AudioSource Jump;
public Animator animator;
void HandleMovement(float horizontal)
{
transform.position += new Vector3(horizontal, 0, 0) * Time.deltaTime * MovementSpeed;
animator.SetFloat("velX", (horizontal));
}
void Flip(float horizontal)
{
if (horizontal < 0 && !facingRight || horizontal > 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
transform.position += new Vector3(horizontal, 0, 0) * Time.deltaTime * MovementSpeed;
horizontalMove = Input.GetAxisRaw("Horizontal") * MovementSpeed;
animator.SetFloat("velX", (horizontalMove));
}
}
IEnumerator ShootingAnim()
{
animator.SetBool("IsShooting", true);
yield return new WaitForSeconds(0.2f);
animator.SetBool("IsShooting", false);
}
void Update()
{
animator.SetFloat("velX", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
animator.SetBool("IsJumping", true);
// Jump.Play();
}
if (Mathf.Abs(_rigidbody.velocity.y) == 0f)
{
animator.SetBool("IsJumping", false);
}
if ((_rigidbody.velocity.x) >= 0.01f || (_rigidbody.velocity.x <= -0.01f))
{
animator.SetBool("IsRunning", true);
}
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
StartCoroutine(ShootingAnim());
nextFire = Time.time + fireRate;
Fire();
}
float horizontal = Input.GetAxis("Horizontal");
HandleMovement(horizontal);
Flip(horizontal);
}
void Fire()
{
bulletPos = spawnPoint.position;
if (!facingRight)
{
bulletPos += new Vector2(+0.02f, 0f);
Instantiate(bulletToRight, bulletPos, Quaternion.identity);
Gunshot.Play();
} else
{
bulletPos += new Vector2(-0.02f, 0f);
Instantiate(bulletToLeft, bulletPos, Quaternion.identity);
Gunshot.Play();
}
}
}
这是 BulletScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public GameObject Bullet;
public Transform spawnPoint;
public Rigidbody2D bulletRB;
public float velX = 5f;
float velY = 0f;
// Start is called before the first frame update
void Start()
{
bulletRB = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D Other)
{
Destroy(this.gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
this.gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
private void Update()
{
bulletRB.velocity = new Vector2(velX, velY);
Destroy(gameObject, 3f);
}
}