콜라이더와 리지드바디로 깃발에 충돌하면 멈추게 함

차가 움직일 때, 사운드 컴포넌트를 실행함

 

  • car
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Car : MonoBehaviour
{
    float moveSpeed;
    Vector2 startPos;
    Vector2 endPos;

    void Update()
    {
        if(Input.GetMouseButtonDown(0))  // 마우스를 누른 곳의 좌표를 구한다.
        {
           this.startPos = Input.mousePosition;
        }else if(Input.GetMouseButtonUp(0))    // 마우스를 뗀 곳의 좌표를 구한다.
        {
           this.endPos = Input.mousePosition;

        // 마우스를 뗀 곳의 좌표에서 누른 곳의 좌표를 빼서 양수로 변환해 스피드에 넣는다.
        this.moveSpeed = Mathf.Abs((this.endPos.x - this.startPos.x)/10000);
        Debug.Log(moveSpeed);

        this.GetComponent<AudioSource>().Play(); // 사운드 컴포넌트를 실행함
        }

        transform.Translate(moveSpeed , 0, 0);

        moveSpeed *= 0.99f;
    }
}

 

 

  • UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class carDistance : MonoBehaviour
{
    public GameObject Car;
    public GameObject Flag;
    public GameObject distance;
   
    void Update()
    {
        float legth = this.Flag.transform.position.x - Car.transform.position.x;
       // Debug.Log(legth);

        this.distance.GetComponent<Text>().text = legth.ToString("F2")+"m";
    }
}

'└▶연습' 카테고리의 다른 글

고양이 점프  (6) 2024.09.16
고양이 화살 피하기 3단계  (6) 2024.09.16
고양이 화살 피하기 2단계  (1) 2024.09.16
고양이 화살 피하기 1단계  (2) 2024.09.15
룰렛 기본형  (0) 2024.09.15

+ Recent posts