2016-06-23 7 views
1

나는 튀는 공을 프로그램하고 싶다. 실제 바운스는 실제 물리 법칙을 따르지 않고 절대 사인파를 만들어야합니다. 마찬가지로 지금까지 꽤 쉽게이 이미지사인파를 느리게하는 방법?

enter image description here

인치 첨부 된 코드를 참조하십시오.

그러나 버튼을 누르면 곧 내 공의 움직임을 느리게하고 싶습니다.

x 축에서 변환 속도가 느려지는 이동 속도가 줄어 듭니다. 그러나 사인파의 주파수 나 크기가 변경되지 않았기 때문에 볼은 여전히 ​​빠르게 위아래로 튀고 있습니다. 그러나 하나 또는 둘 모두를 바꿀 때, 이것은 이상한 행동으로 끝날 것입니다.

공이 위에서 아래로 20 % 떨어진 거리에서 내려 가기를한다고 상상해보십시오. 이제 사인 파라미터를 변경하면 볼이 즉시 y 축을 따라 움직입니다. y 축은 정말 이상하고 부드럽지 않습니다.

내 실제 질문 : 고르지 않은 움직임없이 절대 사인파 (x/y 축 변환)를 늦추려면 어떻게해야합니까? (-, 더 블루 낮은 주파수 - 더 레드 이상)

float _movementSpeed = 10.0f; 
float _sineFrequency = 5.0f; 
float _sineMagnitude = 2.5f; 

Vector3 _axis; 
Vector3 _direction; 
Vector3 _position; 

void Awake() 
{ 
    _position = transform.position; 
    _axis = transform.up; 
    _direction = transform.right; 
} 

void Update() 
{ 
    _position += _direction * Time.deltaTime * _movementSpeed; 
    // Time.time is the time since the start of the game 
    transform.position = _position + _axis * Mathf.Abs(Mathf.Sin(Time.time * _sineFrequency)) * _sineMagnitude; 
} 
+0

y 방향의 속도가 전적으로 x 방향의 속도에 의존하지 않습니까? – Dziugas

답변

0

난 당신이 원하는 것 같아요 :

A graph of a sine wave in time with changing frequency 이를 위해 가장 쉬운 방법이이 생성 된 예를 들어, deltatime 확장하는 것입니다 :

using UnityEngine; 
using System.Collections; 

public class Bounce : MonoBehaviour { 

    // for visualisation of results 
    public Graph graph; 

    [Range(0, 10)] 
    public float frequency = 1; 

    float t; 

    void Update() 
    { 
     t += Time.deltaTime * frequency; 

     float y = Mathf.Abs(Mathf.Sin(t)); 
     float time = Time.realtimeSinceStartup; 

     graph.AddPoint(time, y, Color.Lerp(Color.red, Color.blue, frequency/10)); 
    } 

} 

그리고 완전성에 대한

Graph 클래스 :

using UnityEngine; 
using System.Collections.Generic; 

public class Graph : MonoBehaviour { 

    struct PosAndColor 
    { 
     public Vector3 pos; 
     public Color color; 
    } 


    List<PosAndColor> values=new List<PosAndColor>(); 


    public void AddPoint(float x, float y, Color c) 
    { 
     values.Add(new PosAndColor() { pos = new Vector3(x, y, 0), color = c }); 
    } 


    void OnDrawGizmos() 
    { 
     for (int i = 1; i < values.Count; i++) 
     { 
      Gizmos.color = values[i - 1].color; 
      Gizmos.DrawLine(values[i - 1].pos, values[i].pos); 
     } 
    } 

} 

다른 진폭은 현재 진폭과 일치하도록 사인 오프셋을 사용하여 주파수를 변경하는 것입니다. 나는 네가 여기 이걸 필요하다고 생각하지 않지만, 네가 원하면 나는 그 주제에 대해 좀 더 올릴 수있어.

관련 문제