本文介绍了当玩家移动时,Unity 如何让我的枪停止射击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个枪射击脚本,但是我不知道如何在播放器运行时禁用射击.有人可以帮我吗?
I am making a gun shooting script, but I do not know how to disable shooting when the player is running. Can someone help me with that?
void Shoot()
{
MuzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Range))
{
Debug.Log(hit.transform.name);
EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
if (enemy != null)
{
enemy.TakeDamage(Damage);
}
}
}
这是我的角色控制器脚本的移动部分:
Here is the movement section of my character controller script:
void Movement()
{
transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
WalkSpeed = RunSpeed;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
WalkSpeed = DefaultSpeed;
}
}
推荐答案
您可以像这样在其他脚本中设置变量:
You can set variables in other scripts like this:
[Serialize Field]
private GameObject objectWithYourScriptYouNeed;
private ClassOfScriptYouNeed pS;
void Start()
{
pS = objectWithYourScript.GetComponent<ClassOfScriptYouNeed>();
pS.varName = 12345;
}
因此,在您的拍摄脚本中:
So, in your shoot script:
Public Bool isMoving;
void Shoot()
{
//I'm assuming you also dont want muzzleflash to play.
if (isMoving != true)
{
MuzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Range))
{
Debug.Log(hit.transform.name);
EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
if (enemy != null)
{
enemy.TakeDamage(Damage);
}
}
}
}
你的动作脚本:
//put your shooting scripts name below.
private ShootScript shootScript;
void start()
{
shootScript = GetComponent<ShootScript>();
}
void Movement()
{
shootScript.isMoving = Input.GetAxis("Vertical") != 0f
|| Input.GetAxis("Horizontal") != 0f ;
transform.Translate(Vector3.forward * Input.GetAxis("Vertical")
* WalkSpeed * Time.deltaTime);
transform.Translate(Vector3.right * Input.GetAxis("Horizontal")
* WalkSpeed * Time.deltaTime);
// GetKey is true on every frame that shift is held down, and false when it isn't
// GetKeyDown is only true on the first frame in a row that shift is held down
if (Input.GetKey(KeyCode.LeftShift))
{
WalkSpeed = RunSpeed;
}
else
{
WalkSpeed = DefaultSpeed;
}
}
这篇关于当玩家移动时,Unity 如何让我的枪停止射击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!