강아지(나)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move: MonoBehaviour
{
Rigidbody2D rb;
private float moveH, moveV;
public float moveSpeed=2.0f;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
moveH=Input.GetAxisRaw("Horizontal");
moveV =Input.GetAxisRaw("Vertical");
moveSpeed += 0.0002f;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "sheep")
{
Destroy(gameObject);
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2( moveH, moveV).normalized * moveSpeed;
}
}
적(양)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sheep_stoker : MonoBehaviour
{
public string targetObjectName="Player";
public float speed = 0.01f;
public int at = 1;
GameObject targetObject;
Rigidbody2D rbody;
public bool a=false;
void Update(){
if(a){
gameObject.SetActive(true);
}else{
gameObject.SetActive(false);
}
speed += 0.0001f;
}
// Start is called before the first frame update
void Start()
{
speed = 0.01f;
// 추격할 타겟 오브젝트 이름을 넣는다.
targetObject = GameObject.Find(targetObjectName);
rbody = GetComponent<Rigidbody2D>();
rbody.gravityScale = 0; //중력 무시
rbody.constraints = RigidbodyConstraints2D.FreezeRotation; //Z축 회전무시
}
// Update is called once per frame
void FixedUpdate()
{
// 추격할 타겟 오브젝트의 방향을 구하기.(normalized : 방향은 유지하고 길이가 1인벡터 생성. 일정한 속도로 쫓아감)
Vector3 dir = (targetObject.transform.position - this.transform.position).normalized;
float vx = dir.x * speed;
float vy = dir.y * speed;
// 해당 방향으로의 속도를 세팅하기
rbody.velocity = new Vector2(vx, vy);
//x축을 넘어가면 반전하기
}
}
잔디,텐트,바오밥 나무