using UnityEngine;
using System.Collections;
public class PlayerControls06 : MonoBehaviour {
public bool facingRight = true; // For determining which way the player is currently facing.
public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 5f; // The fastest the player can travel in the x axis.
public float jumpForce = 1000f;
public bool jump = false;
private bool grounded = false;
private Transform groundCheck;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
groundCheck = transform.Find("groundCheck");
}
// Update is called once per frame
void Update () {
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
float h = Input.GetAxis("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (h));
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if(h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
// ... add a force to the player.
GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
if(Input.GetButtonDown("Jump") && grounded)
jump = true;
if(jump)
{
anim.SetTrigger("Jump");
// Add a vertical force to the player.
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
// Make sure the player can't jump again until the jump conditions from Update are satisfied.
jump = false;
}
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
void Start() 대신에 Awake() 사용
anim = GetComponent<Animator>();를 Awake로 옮김
기존의 Update() 부분을 Update()와 FixedUpdate()로 나눔
Update : "Jump"키만 눌렸는지를 확인하도록 함
FixedUpdate : 물리 연산과 관련된 모든 소스 코드를 이쪽으로 옮김
Physics2D.Linecast()의 세번째 인자 [ 1 << LayerMask.NameToLayer("Ground")]는 결국 Gound라는 Layer에 존재하는 Collider들만을 체크하게 하는데 사용됨
즉, 점프가 가능한 객체(UFO, 땅 등)는 Ground Layer에 속해 있어야 함(Tag가 아니라 Layer임)
using UnityEngine;
using System.Collections;
public class Gun01 : MonoBehaviour {
private Animator anim; // Reference to the Animator component.
// Use this for initialization
void Awake () {
anim = transform.root.gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
print ("Fire.....");
// ... set the animator Shoot trigger parameter and play the audioclip.
anim.SetTrigger("Shoot");
}
}
}
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
public Rigidbody2D rocket; // Prefab of the rocket.
public float speed = 20f; // The speed the rocket will fire at.
private PlayerControls playerCtrl; // Reference to the PlayerControl script.
private Animator anim; // Reference to the Animator component.
AudioSource audio;
void Awake()
{
// Setting up the references.
anim = transform.root.gameObject.GetComponent<Animator>();
playerCtrl = transform.root.GetComponent<PlayerControls>();
audio = GetComponent<AudioSource>();
}
void Update ()
{
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
// ... set the animator Shoot trigger parameter and play the audioclip.
anim.SetTrigger("Shoot");
audio.Play ();
// If the player is facing right...
if(playerCtrl.facingRight)
{
// ... instantiate the rocket facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
else
{
// Otherwise instantiate the rocket facing left and set it's velocity to the left.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(-speed, 0);
}
}
}
}
미사일 속도를 여기서 설정하고 있다.[ bulletInstance.velocity = new Vector2(speed, 0) ] 우린 이미 rocket.cs의 update()에서 미사일이 발사 될 수 있도록 해 놨기 때문에 이부분은 삭제하거나, 위와 같이 넣으려면 rocket.cs의 update()에서 [ transform.Translate (new Vector3(0.1f, 0,0)); ] 부분을 삭제해야 함
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreNew : MonoBehaviour
{
public Text scoreText; //Canvas의 text를 넣어주세요.
private int score;
// Use this for initialization
void Start()
{
score = 0;
}
public void RenderScore(int scoreValue) //표시하고자 하는 점수를 표시
{
score += scoreValue;
scoreText.text = "SCORE : " + score;
}
}
---------------------------------------- 아래는 이전 버전 ---------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
public class Score01 : MonoBehaviour {
public int score = 0; // The player's score.
private PlayerControl playerControl; // Reference to the player control script.
private int previousScore = 0; // The score in the previous frame.
void Awake ()
{
// Setting up the reference.
playerControl = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerControl>();
}
void Update ()
{
// Set the score text.
guiText.text = "Score: " + score;
// If the score has changed...
//if(previousScore != score)
// ... play a taunt.
//playerControl.StartCoroutine(playerControl.Taunt());
// Set the previous score to this frame's score.
//previousScore = score;
}
}
using UnityEngine;
using System.Collections;
public class ScoreShadow : MonoBehaviour
{
public GameObject guiCopy; // A copy of the score.
void Awake()
{
// Set the position to be slightly down and behind the other gui.
Vector3 behindPos = transform.position;
behindPos = new Vector3(guiCopy.transform.position.x, guiCopy.transform.position.y-0.005f, guiCopy.transform.position.z-1);
transform.position = behindPos;
}
void Update ()
{
// Set the text to equal the copy's text.
guiText.text = guiCopy.guiText.text;
}
}
guiText와 guiTexture는 다른 GameObject들과 다르게 GUI 좌표계를 사용하므로, 상대적으로 z값이 더 큰 것이 화면의 위쪽에 위치하게 된다.
(참고 : http://moguwai.tistory.com/entry/GUITexture%EC%9D%98-%EC%A2%8C%ED%91%9C)