- 자습서
- Space Shooter tutorial
- Shooting shots
Shooting shots
확인 완료한 버전: 5.1
-
난이도: 초급
Writing the code and setting up the scene to shoot shots.


Shooting shots
초급 Space Shooter tutorial
Please note that this project was developed for Unity version 4. It currently works, and has been checked, with Unity version 5.1. Please turn annotations on while watching the video, as we can add annotations to the videos when there are discrepancies between the recorded content and the current version of Unity.
Get the Upgrade Guide for Unity 5 here.
Please refer to the Official Q&A Page on the Forums for an Upgrade FAQ and to ask any questions.
PlayerController
Code snippet
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
class Boundary
{
var xMin : float;
var xMax : float;
var zMin : float;
var zMax : float;
}
var speed : float;
var tilt : float;
var boundary : Boundary;
var shot : GameObject;
var shotSpawn : Transform;
var fireRate : float;
private var nextFire : float;
function Update () {
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
function FixedUpdate () {
var moveHorizontal : float= Input.GetAxis ("Horizontal");
var moveVertical : float= Input.GetAxis ("Vertical");
var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
import UnityEngine
import System.Collections
[System.Serializable]
public class Boundary:
public xMin as single
public xMax as single
public zMin as single
public zMax as single
public class PlayerController(MonoBehaviour):
public speed as single
public tilt as single
public boundary as Boundary
public shot as GameObject
public shotSpawn as Transform
public fireRate as single
private nextFire as single
private def Update():
if Input.GetButton('Fire1') and (Time.time > nextFire):
nextFire = (Time.time + fireRate)
Instantiate(shot, shotSpawn.position, shotSpawn.rotation)
private def FixedUpdate():
moveHorizontal as single = Input.GetAxis('Horizontal')
moveVertical as single = Input.GetAxis('Vertical')
movement = Vector3(moveHorizontal, 0.0F, moveVertical)
rigidbody.velocity = (movement * speed)
rigidbody.position = Vector3(Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0F, Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax))
rigidbody.rotation = Quaternion.Euler(0.0F, 0.0F, (rigidbody.velocity.x * (-tilt)))
관련 자습서
- Instantiate (강좌)
- Quaternions (강좌)
- GetButton and GetKey (강좌)
관련 문서
- Input (설명서)
- Input Manager (설명서)
- Time (스크립팅 참고자료)
- Input (스크립팅 참고자료)
- Input.GetButton (스크립팅 참고자료)
- Instantiating Prefabs at runtime (설명서)
- Instantiate (스크립팅 참고자료)
- Quaternion (스크립팅 참고자료)
- Quaternion.Euler (스크립팅 참고자료)
- Quaternion.eulerAngles (스크립팅 참고자료)