교제 6장복습
#6장
(Physics,메카님)
#Physics
*Rigidbody
중력,마찰
중력의 영향을 받고싶지 않다면?
body type-Kinematic설정
*Collider
충돌 판정,충격
회전축 방지?
Constaints(x,y,z)
*Rigidbody2D 변수명;
.AddForce(transform.up) ->Rigidbody를 사용하려면 AddForce로 입력값을 준다
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Rigidbody2D.AddForce.html
Unity - Scripting API: Rigidbody2D.AddForce
The force is specified as two separate components in the X and Y directions (there is no Z direction in 2D physics). The object will be accelerated by the force according to the law force = mass x acceleration - the larger the mass, the greater the force r
docs.unity3d.com
void Update()
{
rb2D.AddForce(transform.up * thrust);
}
#Mathf.Abs=>Returns the absolute value of f.(value값 반환)
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Mathf.Abs.html
Unity - Scripting API: Mathf.Abs
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see: You've told us there are code samples on this page which don't work. If you know ho
docs.unity3d.com
#localScale //이미지 반전(ex)transform.localScale = new Vector3(key, 1, 1);
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Transform-localScale.html
Unity - Scripting API: Transform.localScale
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see: You've told us there are code samples on this page which don't work. If you know ho
docs.unity3d.com
#play의 jump,좌우이동,속도,속도제한,이미지반전
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
float jumpFoce = 300.0f;
float walkForce = 30.0f;
float maxWalkForce = 2.0f;
// Start is called before the first frame update
void Start()
{
this.rigid2D= GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//space ->jump!
if(Input.GetKeyDown(KeyCode.Space))
{
this.rigid2D.AddForce(transform.up*this.jumpFoce);
//.AddForce(transform.up*jumpForce)
}
//좌우 이동
int key = 0;
if (Input.GetKey(KeyCode.RightArrow)) key= 1;
if (Input.GetKey(KeyCode.LeftArrow)) key = -1;
//player의 속도
float speedx=Mathf.Abs(this.rigid2D.velocity.x); //Abs 값 반환 //.velocity속도.x
//player의 이동속도 제한
if(speedx<this.maxWalkForce)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
//방향에 따라 이미지 반전
if(key!=0)
{
transform.localScale = new Vector3(key, 1, 1);
}
}
}
#메카님
애니메이션을 작성하고 실행할 때 유니티 에디션에서
'일관되게' 조작할수있는 기능
#Animator animator; 와Animation animator; 오타 주의!(Animation 은 .speed가 없다..)
speed | The playback speed of the Animator. 1 is normal playback speed. |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
Animator animator;
float jumpFoce = 300.0f;
float walkForce = 30.0f;
float maxWalkSpeed = 2.0f;
// Start is called before the first frame update
void Start()
{
this.rigid2D= GetComponent<Rigidbody2D>();
this.animator= GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//space ->jump!
if(Input.GetKeyDown(KeyCode.Space))
{
this.rigid2D.AddForce(transform.up*this.jumpFoce);
//.AddForce(transform.up*jumpForce)
}
//좌우 이동
int key = 0;
if (Input.GetKey(KeyCode.RightArrow)) key= 1;
if (Input.GetKey(KeyCode.LeftArrow)) key = -1;
//player의 속도
float speedx=Mathf.Abs(this.rigid2D.velocity.x); //Abs 값 반환 //.velocity속도.x
//player의 이동속도 제한
if(speedx<this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
//방향에 따라 이미지 반전
if(key!=0)
{
transform.localScale = new Vector3(key, 1, 1);
}
//player속도에 맞춰서 애니메이션 속도 변경
this.animator.speed = speedx / 2.0f;
}
}
#player의 이동에 따라 카메라 움직임 이동
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
GameObject player;
// Start is called before the first frame update
void Start()
{
//this.player = GetComponent<GameObject>();
this.player = GameObject.Find("cat");
}
// Update is called once per frame
void Update()
{
Vector3 playerPos=this.player.transform.position;
transform.position=new Vector3(transform.position.x,playerPos.y, transform.position.z); //playerPos의y축을 따라 움직임
}
}
#Physics 충돌 판정
*Collision모드(충돌) / Trigger모드(통과)
*충돌한 순간-Collision모드(OnCollisionEnter2D)/Trigger모드(OnTriggerEnter2D)
충돌 중-Collision모드(OnCollisionStay2D)/Trigger모드(OnTriggerStay2D)
충돌 끝-Collision모드(OnCollisionExit2D)/Trigger모드(OnTriggerExit2D)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
Animator animator;
float jumpFoce = 300.0f;
float walkForce = 30.0f;
float maxWalkSpeed = 2.0f;
// Start is called before the first frame update
void Start()
{
this.rigid2D= GetComponent<Rigidbody2D>();
this.animator= GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//space ->jump!
if(Input.GetKeyDown(KeyCode.Space))
{
this.rigid2D.AddForce(transform.up*this.jumpFoce);
//.AddForce(transform.up*jumpForce)
}
//좌우 이동
int key = 0;
if (Input.GetKey(KeyCode.RightArrow)) key= 1;
if (Input.GetKey(KeyCode.LeftArrow)) key = -1;
//player의 속도
float speedx=Mathf.Abs(this.rigid2D.velocity.x); //Abs 값 반환 //.velocity속도.x
//player의 이동속도 제한
if(speedx<this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
//방향에 따라 이미지 반전
if(key!=0)
{
transform.localScale = new Vector3(key, 1, 1);
}
//player속도에 맞춰서 애니메이션 속도 변경
this.animator.speed = speedx / 2.0f;
}
//flag 와 충돌
void OnTrigger2D(Collider2D other) //충돌 상대 오브젝트를 매개변수로
{
Debug.LogFormat("골");
}
}
#Scene변환
Unity - Scripting API: SceneManagement.SceneManager.LoadScene
Note: In most cases, to avoid pauses or performance hiccups while loading, you should use the asynchronous version of this command which is: LoadSceneAsync. When using SceneManager.LoadScene, the loading does not happen immediately, it completes in the nex
docs.unity3d.com
#씬 등록을 해야 load가 됨
#player가 flag와 충돌하면 씬전환
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; //LoadScende 사용
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
Animator animator;
float jumpFoce = 300.0f;
float walkForce = 30.0f;
float maxWalkSpeed = 2.0f;
// Start is called before the first frame update
void Start()
{
this.rigid2D= GetComponent<Rigidbody2D>();
this.animator= GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//space ->jump!
if(Input.GetKeyDown(KeyCode.Space))
{
this.rigid2D.AddForce(transform.up*this.jumpFoce);
//.AddForce(transform.up*jumpForce)
}
//좌우 이동
int key = 0;
if (Input.GetKey(KeyCode.RightArrow)) key= 1;
if (Input.GetKey(KeyCode.LeftArrow)) key = -1;
//player의 속도
float speedx=Mathf.Abs(this.rigid2D.velocity.x); //Abs 값 반환 //.velocity속도.x
//player의 이동속도 제한
if(speedx<this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
//방향에 따라 이미지 반전
if(key!=0)
{
transform.localScale = new Vector3(key, 1, 1);
}
//player속도에 맞춰서 애니메이션 속도 변경
this.animator.speed = speedx / 2.0f;
}
//flag 와 충돌
void OnTriggerEnter2D(Collider2D other) //충돌 상대 오브젝트를 매개변수로
{
Debug.LogFormat("골");
SceneManager.LoadScene("ClearScene"); //충돌 했다면 씬 전환
}
}
#점프를 두번한다면? 화면 밖으로(y) 이탈한다면?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; //LoadScende 사용
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
Animator animator;
float jumpFoce = 300.0f;
float walkForce = 30.0f;
float maxWalkSpeed = 2.0f;
// Start is called before the first frame update
void Start()
{
this.rigid2D= GetComponent<Rigidbody2D>();
this.animator= GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//space ->jump! //이중 점프 방지?
if(Input.GetKeyDown(KeyCode.Space)&&this.rigid2D.velocity.y==0) //rigid2D의 velocity가 0이어야 다시 점프 가능
{
this.rigid2D.AddForce(transform.up*this.jumpFoce);
//.AddForce(transform.up*jumpForce)
}
//좌우 이동
int key = 0;
if (Input.GetKey(KeyCode.RightArrow)) key= 1;
if (Input.GetKey(KeyCode.LeftArrow)) key = -1;
//player의 속도
float speedx=Mathf.Abs(this.rigid2D.velocity.x); //Abs 값 반환 //.velocity속도.x
//player의 이동속도 제한
if(speedx<this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
//방향에 따라 이미지 반전
if(key!=0)
{
transform.localScale = new Vector3(key, 1, 1);
}
//player속도에 맞춰서 애니메이션 속도 변경
this.animator.speed = speedx / 2.0f;
//플레이어가 화면 밖으로 이탈하면?
if(transform.position.y<-10)
{
SceneManager.LoadScene("GameScene"); //처음부터 다시 시작
}
}
//flag 와 충돌
void OnTriggerEnter2D(Collider2D other) //충돌 상대 오브젝트를 매개변수로
{
Debug.LogFormat("골");
SceneManager.LoadScene("ClearScene"); //충돌 했다면 씬 전환
}
}