Unity2014. 8. 10. 16:35

고정된 카메라가 앞에 놓여 있는 물체 움직임을 추적하며 상하좌우로 회전하게 하려면

http://docs.unity3d.com/ScriptReference/Transform-rotation.html


using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float smooth = 2.0F;
    public float tiltAngle = 30.0F;
    void Update() {
        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    }
}


예제 코드를 테스트하지 않고 수정하여 사용했습니다.

using UnityEngine; using System.Collections; public class OverWatch : MonoBehaviour { public Transform targetCar; private Vector3 offset; public float smooth = 2.0F; public float tiltAngle = 30.0F; // Use this for initialization void Start () { //offset = transform.position; offset.x = this.targetCar.position.x; offset.y = this.targetCar.position.y; } // Update is called once per frame void Update () { //transform.position = target.position + offset; float tiltAroundY = (offset.x - this.targetCar.position.x) * tiltAngle; // ㄱ float tiltAroundX = (offset.y - this.targetCar.position.y) * tiltAngle; // ㄴ Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, 0); // ㄷ transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); //transform.rotation *= target; offset.x = this.targetCar.position.x; offset.y = this.targetCar.position.y; // ㅁ } }




원하는 방향으로 회전하는 결과를 얻을 수 없었습니다. 하는 중에  LookAt함수를 찾았고 ㄱ~ㅁ까지 한 줄로 바꿀 수 있었습니다.

transform.LookAt (this.targetCar.position);


'Unity' 카테고리의 다른 글

유니티 2D 스프라이트  (0) 2014.08.07
Animator 애니메이터 (매카님) 에서 지원하는 파라미터  (0) 2014.08.07
Ray 쏘아 오브젝트 삭제  (0) 2014.08.06
유니티 물체 배치  (0) 2014.08.06
Trail Renderer  (0) 2014.08.06
Posted by 코드버무려
Arduino2014. 8. 10. 10:33


목표의식

시간

비용 지불 의사 ; 보통 차비



목표: ARDU-X 100 인터페이스와 결합한 차량 운전 



토글 버튼 용도와 IR센서 용도


 

 IR센서: 엑셀레이터

 

 왼:좌회전

 

 우:우회전

 

하: 느린 후진 

 


색상 센서 R: 장애물 변화

색상 센서 G: 수플 토글

색상 센서 B: 하늘 변화



  1. Plane 오브젝트를 화면에 배치하고 reset
    1. 크기 조절
  2. Directional Light1를 배치. 각도를 조절해서 그림자 생성을 유도
  3. Directional Light2를 배치. 각도를 조절해서 반사광을 유도했으나 1~3 단계를 30분여 왔다갔다 하면 보류.
  4. 큐브 오브젝트로 벽 세개 북, 동, 서 를 만들었다.
    1. Plane 오브젝트 각 모서리에 배수를 적용해서 크기를 조절하고 위치 잡으려 시도.
      1. Plane에 적용된 크기 값과 큐브에 적용된 위치, 크기 값은 기본 단위가 다르다. 보류.
    2. 각 벽에 텍스쳐를 입힘.
  5. 스크립트를 작성해서 큐브를 이동.
    1. PlayController.cs를 추가해 큐브 이동가능.
  6. 전체적으로 오브젝트가 이동해 있음.
    1. 방향키를 잘 못 눌른 결과인지 유니티 버그인지 알 수 없음.


Posted by 코드버무려
Android 안드로이드2014. 8. 8. 23:10


토글 버튼은 한 번 누르면 상태가 바뀌고 다시 누르면 그 바뀐 상태에서 원래 상태로 되롤아 갑니다.


이것이 익히 생각하는 토글 버튼이고 ToggleButton 클래스로 구현이 쉽게 됩니다. 텍스트 토글 버튼이면 간답합니다. 그러나 텍스트를 이미지로 대체하면 결과는 그렇지 않다는 것을 볼 수 있습니다. 그냥 이미지가 눌러질 때 잠깐 바뀌고 다시 되돌아가는 가짜 토글일 뿐이죠. 


안드로이드 기본 예제나 보통 접할 수 있는 방법으로는 이미지를 클릭하면 원하는 토글 효과를 접할 수 없습니다. 보통 클릭하는 순간 이미지가 바뀌고 손가락이 떨어지는 순간 초기 이미지로 돌아와 버립니다. 이래서는 토글이라고 할 수가 없죠. 이것은 토글 버튼이라기 보다는 딸깍 버튼이라고 부르는 것이 보다 근접한 표현이라 하겠습니다.


여기서는 위에 기술한 형태로 반응하는 버튼이 아니고 익히 생각하는 그 버튼입니다. 이미지 버튼을 클릭이나 터치하면 다른 버튼으로 바뀝니다. 손가락을 떼어도 그 변경된 상태를 그대로 유지합니다. 다시 터치를 하면 이미지는 초기 이미지로 되돌아갑니다. 이것이 진정한 토글 이미지 버튼으로서 여기 블로그를 찾은 이들에게 정확한 답변이 되겠습니다.


아래는 java 소스 변경 없이 xml만을 변경하고 추가합니다.


*************acti_main_tst_toggle.xml 파일 @layout 폴더***************************

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"


    

    <ToggleButton

        android:background="@drawable/bttn_toggle_bg" 

        style="@style/OurThemeName" 

        android:checked="true"

        android:id="@+id/tbt_tst"

   android:layout_width="match_parent"

   android:layout_height="match_parent">

    </ToggleButton>

</RelativeLayout>



*************themes.xml 파일 @values 폴더***************************

<?xml version="1.0" encoding="utf-8"?>

<resources>

<!-- Overwrite the ToggleButton style -->

<style name="Widget.Button.Toggle" parent="android:Widget">

   <item name="android:background">@drawable/bttn_toggle_bg</item>

   <item name="android:textOn">Add</item>

   <item name="android:textOff">Del</item>

   <item name="android:disabledAlpha">?android:attr/disabledAlpha</item>

</style>

 

<style name="OurThemeName"  parent="@android:Theme.Black">

   <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>

<item name="android:textOn"></item>

   <item name="android:textOff"></item>

</style>  

</resources>





                



*************bttn_toggle.xml 파일 @drawable폴더***************************

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_checked="true" android:drawable="@drawable/button_wine" />

<item android:state_checked="false" android:drawable="@drawable/button_wine_off" />

</selector>




*************bttn_toggle_bg.xml 파일 @drawable폴더***************************

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+android:id/background" android:drawable="@android:color/transparent" />

    <item android:id="@+android:id/toggle" android:drawable="@drawable/bttn_toggle" />

</layer-list>

Posted by 코드버무려
Arduino2014. 8. 8. 17:24

//아래 두 코드는 원하는 결과가 나오지 않는다.

// 엣지 풀? 업 , 엣지 풀?다운 을 생각해 코딩한다.


boolean LedState;

int preButton;


void setup()

{

  pinMode(8, OUTPUT);

  pinMode(9, INPUT_PULLUP);

  

  LedState = LOW;

  preButton = digitalRead(9);

}


void loop()

{

  int curButton = digitalRead(9);

  if(curButton == LOW && preButton == HIGH)

  {

    if(LedState == HIGH)  LedState = LOW;

    else LedState = HIGH;

  }

  

  preButton = curButton;

  digitalWrite(8, LedState);  

}



---------------------------------



unsigned long time;

int LedState;


void setup()

{

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);//

  time = millis();

  LedState = LOW;

}


void loop()

{

  unsigned long time2 = millis();

  

  if(time2 - time >= 1000)

  {

    if(LedState == HIGH)

      {LedState = LOW; digitalWrite(9, !LedState); }

    else

      LedState = HIGH;

      

    //digitalWrite(9, LedState);  

    time = time2;

  }

  

  digitalWrite(8, LedState);

}

Posted by 코드버무려
Arduino2014. 8. 8. 15:25


A0입력에 아날로그 다이얼이나 IR센서를 넣는다.

11(11~)번 핀에 모터나 LED를 설치하면 아날로그 결과가 나온다.

void loop()

{

  int dial = analogRead(A0);

  analogWrite(11, dial/4);

}


"~"가 붙은 번호는 아날로그 출력이 가능핟.


---------------------------------------


동기방식은 어렵지만 멀티태스크가능하다.

비동기방식은 쉽지만 멀티태스킹이 불가능하다.  delay()를 사용.


delay(1000); 1000밀리세컨드 = 1초



----------------------------



시리얼 통신


void setup()

{

  Serial.begin(9600);

}


void loop()

{

  Serial.print("hello");

  Serial.println("hello"); // 끝엣 엔털를 쳦줌. New Line

}


-------------------------------


// 9번 핀에 연결된 버튼을 누루는 순간 꺼지고 

// 손가락을 떼면(버튼이 원위치로 뛰어로름) 꺼짐.

//

//  <추측> digitalWrite(8, HIGH/LOW);  에서 

//  Ardu-X 100 Starter Kit에 포함된 버튼은 

// 누를 때 LOW( 0 )값을 발생하고    

// 손가락을 뗄 때 HIGH( 1 )값을 발생한다.


void setup()

{

  pinMode(8, OUTPUT);

  pinMode(9, INPUT_PULLUP);

}


void loop()

{

  int button =  digitalRead(9) ;

  digitalWrite(8, button);  

  //digitalWrite(8, digitalRead(9)); 

}



----------------------------


//IR센서 1개 사용
void setup()

{

  Serial.begin(9600);

}


void loop()

{  

  int irValue = analogRead(A0);

  Serial.println(irValue, DEC);

}



----------------------------


// IR센서 3개 사용

void setup()

{

  Serial.begin(9600);

}


void loop()

{

  int irValue = analogRead(A0);

  Serial.print(irValue, DEC);

  Serial.print(", ");

  

  int irValue1 = analogRead(A1);

  Serial.print(irValue1, DEC);

  Serial.print(", ");


  int irValue2 = analogRead(2);

  Serial.println(irValue2, DEC);

}



---------------------------------

// a버튼을 누르느느 순간에 IR로 값을 받아 출력한다.


void setup()

{

  Serial.begin(9600);

}


void loop()

{

  if(Serial.available() > 0)

  {

    if(Serial.read() == 'a')

    {

      int irValue = analogRead(A0);

      Serial.print(irValue, DEC);

      Serial.print(", ");

  

      int irValue1 = analogRead(A1);

      Serial.print(irValue1, DEC);

      Serial.print(", ");

    

      int irValue2 = analogRead(2);

      Serial.println(irValue2, DEC);

    }

  }

}


------------------------

//stdio.h를 사용해서 간략하게 터미널에 출력


void setup()

{

  Serial.begin(9600);

}


void loop()

{

  if(Serial.available() > 0)

  {

    if(Serial.read() == 'a')

    {

      int irValue = analogRead(A0);  

      int irValue1 = analogRead(A1);    

      int irValue2 = analogRead(2);

       printf("IR1:%d,   IR2:%d,    IR3:%d\n", irValue, irValue1, irValue2);

    }

  }

}


Posted by 코드버무려