using UnityEngine;
using System.Collections;
public class PlayerControls01 : MonoBehaviour {
public float speed = 10.0f;
void Update () {
float h = Input.GetAxis("Horizontal");
transform.position += transform.right*h*speed*Time.deltaTime;
}
}
using UnityEngine;
using System.Collections;
public class PlayerControls02 : MonoBehaviour {
public float maxSpeed = 5f; // The fastest the player can travel in the x axis.
public float moveForce = 365f; // Amount of force added to move the player left and right.
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
// 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);
}
}
CharacterController를 사용하고 있지 않고 Rigidbody에 Addforce를 사용해서 움직이게 하고 있음
Addforce() : 힘을 가하는 함수
using UnityEngine;
using System.Collections;
public class PlayerControls03 : 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;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
// 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"))
jump = true;
if(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;
}
}
using UnityEngine;
using System.Collections;
public class PlayerControls04 : 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 Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
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"))
jump = true;
if(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;
}
}
using UnityEngine;
using System.Collections;
public class PlayerControls05 : 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 Animator anim;
// Use this for initialization
void Start () {
// Setting up references.
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
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"))
jump = true;
if(jump)
{
// Set the Jump animator trigger parameter.
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;
}
}
Horizontal 입력 값이 0보다 높은지 낮은지 확인하여 캐릭터가 어느 방향을 바라보게 만들지 결정
입력에 따라 캐릭터의 X축을 역전 시켜 캐릭터를 반대 방향으로 바꾸는 Flip() 를 호출