C# 我的统一角色需要什么代码才能正确射击?
我对编程并不陌生,但我对C#还是新手。我有Python、Java和HTML方面的经验。我的游戏是2D,我有一个游戏,我的角色现在必须接触敌人才能杀死它。现在我添加了射杀敌人的代码。如果按下空格键,我也希望子弹被射中。角色应该向任意方向发射子弹。我从教授给我的一个示例中提取了我的代码,该示例最初是Javascript,我将其转换为C。Unity不再支持Javascript。他给我的示例代码基本上是一个火箭,它射出的子弹和我点击的一样多(点击鼠标射出的子弹),以消灭一个敌人,但是该示例中的火箭不移动。在我的游戏中,角色移动,因此子弹必须获得角色的位置。获取角色位置和正确射击子弹的正确代码是什么 我用我当前的代码测试了我的游戏。子弹是随处吐痰(从我的背景底部的壁纸[在底部的底部打碎]到墙纸下面一点)。甚至从角色的角度都没有。。。 此外,我还在Unity中的Bullet类别中添加了Hit类脚本 完整的摄像机控制器(这里没有任何问题) 完整的玩家控制类(如果你阅读了代码中的注释,上面写着“子弹代码”,这是我在游戏中为子弹添加的新代码C# 我的统一角色需要什么代码才能正确射击?,c#,unity3d,C#,Unity3d,我对编程并不陌生,但我对C#还是新手。我有Python、Java和HTML方面的经验。我的游戏是2D,我有一个游戏,我的角色现在必须接触敌人才能杀死它。现在我添加了射杀敌人的代码。如果按下空格键,我也希望子弹被射中。角色应该向任意方向发射子弹。我从教授给我的一个示例中提取了我的代码,该示例最初是Javascript,我将其转换为C。Unity不再支持Javascript。他给我的示例代码基本上是一个火箭,它射出的子弹和我点击的一样多(点击鼠标射出的子弹),以消灭一个敌人,但是该示例中的火箭不移动
using UnityEngine;
using System.Collections;
//Adding this allows us to access members of the UI namespace including Text.
using UnityEngine.UI;
public class CompletePlayerController : MonoBehaviour
{
public float speed; //Floating point variable to store the player's movement speed.
public Text countText; //Store a reference to the UI Text component which will display the number of pickups collected.
public Text winText; //Store a reference to the UI Text component which will display the 'You win' message.
private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.
private int count; //Integer to store the number of pickups collected so far.
Rigidbody2D bullet;
float speed2 = 30f; //BULLET CODE
// Use this for initialization
void Start()
{
//Get and store a reference to the Rigidbody2D component so that we can access it.
rb2d = GetComponent<Rigidbody2D> ();
//Initialize count to zero.
count = 0;
//Initialze winText to a blank string since we haven't won yet at beginning.
winText.text = "";
//Call our SetCountText function which will update the text with the current value for count.
SetCountText ();
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
//Store the current horizontal input in the float moveHorizontal.
float moveHorizontal = Input.GetAxis ("Horizontal");
//Store the current vertical input in the float moveVertical.
float moveVertical = Input.GetAxis ("Vertical");
Rigidbody2D bulletInstance; //BULLET CODE
//Use the two store floats to create a new Vector2 variable movement.
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
rb2d.AddForce (movement * speed);
if(Input.GetKeyDown(KeyCode.Space)&& Hit.hit == false) //BULLET CODE IN HERE
{
// ... instantiate the bullet facing right and set it's velocity to the right.
bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0)));
bulletInstance.velocity = new Vector2(speed2, 0);
bulletInstance.name = "Bullet";
}
}
//OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
void OnTriggerEnter2D(Collider2D other)
{
//Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
if (other.gameObject.CompareTag ("PickUp"))
{
//... then set the other object we just collided with to inactive.
other.gameObject.SetActive(false);
transform.localScale += new Vector3(0.1f, 0.1f, 0);
//Add one to the current value of our count variable.
count = count + 1;
//Update the currently displayed count by calling the SetCountText function.
SetCountText ();
}
}
//This function updates the text displaying the number of objects we've collected and displays our victory message if we've collected all of them.
void SetCountText()
{
//Set the text property of our our countText object to "Count: " followed by the number stored in our count variable.
countText.text = "Count: " + count.ToString ();
//Check if we've collected all 12 pickups. If we have...
if (count >= 12)
//... then set the text property of our winText object to "You win!"
winText.text = "You win!";
}
}
使用UnityEngine;
使用系统集合;
//添加它允许我们访问UI名称空间的成员,包括文本。
使用UnityEngine.UI;
公共类CompletePlayerController:MonoBehavior
{
public float speed;//用于存储播放器移动速度的浮点变量。
public Text countText;//存储对UI文本组件的引用,该组件将显示收集的拾取数。
public Text winText;//存储对UI文本组件的引用,该组件将显示“You win”消息。
private Rigidbody2D rb2d;//存储对使用2D物理所需的Rigidbody2D组件的引用。
private int count;//用于存储到目前为止收集的拾取数的整数。
刚体2D子弹;
浮动速度2=30f;//项目符号代码
//用于初始化
void Start()
{
//获取并存储对Rigidbody2D组件的引用,以便我们可以访问它。
rb2d=GetComponent();
//将计数初始化为零。
计数=0;
//将winText初始化为空字符串,因为我们在开始时还没有这样做。
winText.text=“”;
//调用我们的SetCountText函数,该函数将使用count的当前值更新文本。
SetCountText();
}
//FixedUpdate以固定的时间间隔调用,与帧速率无关。请将物理代码放在此处。
void FixedUpdate()
{
//将当前水平输入存储在浮点移动水平中。
float moveHorizontal=Input.GetAxis(“水平”);
//将当前垂直输入存储在浮动移动垂直中。
float moveVertical=Input.GetAxis(“垂直”);
Rigidbody2D bulletInstance;//项目符号代码
//使用两个存储浮动创建新的Vector2变量移动。
矢量2移动=新矢量2(水平移动、垂直移动);
//调用刚体2d rb2d的AddForce函数,提供运动乘以速度来移动玩家。
rb2d.AddForce(移动*速度);
if(Input.GetKeyDown(KeyCode.Space)&&Hit.Hit==false)//这里的项目符号代码
{
//…实例化面向右侧的子弹,并将其速度设置为右侧。
bulletInstance=实例化(bullet,transform.position,Quaternion.Euler(新向量3(0,0,0));
bulletInstance.velocity=新矢量2(速度2,0);
bulletInstance.name=“Bullet”;
}
}
//每当此对象与触发器碰撞器重叠时,将调用OnTriggerEnter2D。
无效OnTiggerEnter2D(碰撞的R2D其他)
{
//检查提供的碰撞R2D参数other,查看是否标记为“拾取”,如果是。。。
if(other.gameObject.CompareTag(“拾取”))
{
//…然后将刚刚碰撞的另一个对象设置为非活动。
other.gameObject.SetActive(false);
transform.localScale+=新矢量3(0.1f,0.1f,0);
//将一个添加到count变量的当前值。
计数=计数+1;
//通过调用SetCountText函数更新当前显示的计数。
SetCountText();
}
}
//此函数更新显示我们收集的对象数量的文本,如果我们收集了所有对象,则显示我们的胜利消息。
void SetCountText()
{
//将countText对象的text属性设置为“Count:”,后跟存储在Count变量中的数字。
countText.text=“Count:+Count.ToString();
//检查我们是否收集了所有12辆皮卡。如果我们有。。。
如果(计数>=12)
//…然后将winText对象的text属性设置为“You win!”
winText.text=“你赢了!”;
}
}
命中代码。该类中的所有代码都是为该项目符号生成的
using UnityEngine;
using System.Collections;
public class Hit : MonoBehaviour
{
GameObject[] gameObjects;
public static bool hit = false;
void Removal ()
{
gameObjects = GameObject.FindGameObjectsWithTag("Bullet");
for(var i= 0 ; i < gameObjects.Length ; i ++)
Destroy(gameObjects[i]);
}
void OnCollisionEnter2D ( Collision2D other )
{
if(other.gameObject.name=="Bullet")
{
Removal();
Destroy(gameObject);
hit = true;
}
}
}
使用UnityEngine;
使用系统集合;
公共课热门:单一行为
{
游戏对象[]游戏对象;
公共静态bool hit=false;
空洞清除()
{
gameObjects=GameObject.FindGameObjectsSwithTag(“子弹”);
对于(var i=0;i
问题可能与玩家的父级转换有关。您可以
using UnityEngine;
using System.Collections;
public class Hit : MonoBehaviour
{
GameObject[] gameObjects;
public static bool hit = false;
void Removal ()
{
gameObjects = GameObject.FindGameObjectsWithTag("Bullet");
for(var i= 0 ; i < gameObjects.Length ; i ++)
Destroy(gameObjects[i]);
}
void OnCollisionEnter2D ( Collision2D other )
{
if(other.gameObject.name=="Bullet")
{
Removal();
Destroy(gameObject);
hit = true;
}
}
}
bulletInstance = Instantiate(bullet);
bulletInstance.transform.parent = transform.parent;
bulletInstance.transform.position = transform.position;
bulletInstance.velocity = new Vector2(speed2, 0);
bulletInstance.name = "Bullet";
if (Input.GetKeyDown(KeyCode.Space)) //Shoot bullet
{
// ... instantiate the bullet facing right and set it's velocity to the right.
Rigidbody2D bulletRB = Instantiate(bullet, transform.position, transform.rotation);
bulletRB.AddForce(new Vector2(speed2,0), ForceMode2D.Impulse);
}
public class Bullet : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
Destroy(collision.gameObject);
Destroy(this.gameObject);
}
}