프로그래밍에 익숙하지는 않지만 C#을 처음 사용합니다. 나는 Python, Java, HTML로 경험이있다. 내 게임은 2D입니다. 내 캐릭터가 현재 적을 만져서 죽이는 게임이 있습니다. 이제 적을 죽이기 위해 총알을 쏘는 코드를 추가했습니다. 스페이스 바를 누르면 총알이 발사되기를 바랄뿐입니다. 캐릭터는 어느 방향으로나 총알을 맞았습니다. 교수님이 저에게 준 Javascript 예제에서 코드를 가져 와서 C#으로 변환했습니다. Unity는 더 이상 자바 스크립트를 지원하지 않습니다. 그가 내게 준 예제 코드는 기본적으로 로켓을 총알처럼 쏘아 발사 한 것입니다 (마우스를 클릭하면 총알이 발사 됨). 적을 없애기 위해 발사 한 것이지만, 그 예에서 로켓은 움직이지 않습니다. 내 게임에서 캐릭터가 움직이므로 총알은 캐릭터의 위치를 얻어야합니다. 캐릭터의 위치를 파악하고 총알을 정확히 쏠 수있는 올바른 코드는 무엇입니까?글 머리 기호를 올바르게 촬영하려면 유니티 캐릭터에 필요한 코드는 무엇입니까?
나는 내 게임을 현재 코드로 테스트했다. 총알이 아무 곳에도 뱉어 내지 않고 있습니다. (내 배경 벽지의 아래쪽에서부터 아래쪽의 중간에 가볍게 두드려서 벽지 아래까지). 캐릭터로부터조차 ... 또한 Unity에서 Bullet 카테고리에 Hit 클래스 스크립트를 추가했습니다.
전체 카메라 컨트롤러 (여기에 전혀 문제)
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour {
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}
전체 플레이어 컨트롤 클래스 (당신이 "BULLET 코드"를 말하는 코드의 주석을 읽는다면, 즉 내가에 배치 한 새로운 코드의 총알을위한 게임.
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!";
}
}
히트 코드.이 클래스의 모든 코드는 총알을 위해 만들어졌다.
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;
}
}
}
게임이 3D가 아닌 2D임을 지정할 수 있습니다. –