Question by
alien666 · Apr 10, 2017 at 06:12 PM ·
problem during runtime
Circular motion. Problem
Hello, wrote a mini-test of the motor reaction of a person. But it is not exact. When stopping the movement, the time is shown the exact but the mark behind the circle. please help me heare code:
public class RotationSphere : MonoBehaviour {
private bool _pause = false;
private bool _event_click = false;
private int _one_click = 0;
private int _count_click = 0;
private float _delta = 0f;
private float _minutes = 0f;
private float _seconds = 0f;
private float _startTime = 0f;
private float _actualDeltaTime = 0f;
private float _speed = -2*Mathf.PI;
private float _angle = Mathf.PI/2;
private float[] _array_results = new float[5]; //int count_res = 10;
public int radius = 5;
public float period = 15f;
//position
public float x_pos = 0f;
public float y_pos = 0f;
public float z_pos = 5f;
// Use this for initialization
void Start () {
drawCircle (x_pos, y_pos, z_pos, radius);
_startTime=Time.realtimeSinceStartup;
}
void Awake () {
}
// Update is called once per frame
void Update() {
_angle += _speed*Time.deltaTime/period; //if you want to switch direction, use -= instead of +=
float x = Mathf.Cos(_angle)*radius+x_pos;
float y = Mathf.Sin(_angle)*radius+y_pos;
transform.localPosition = new Vector3 (x, y, z_pos);
if ( y - y_pos > 0 ) {
_event_click = true;
}
else if ( y - y_pos<0 ) {
_event_click = false;
if (_one_click > 0) _one_click = 0;
}
if (Input.GetKeyDown ("up") && _event_click) {
if (_one_click > 0) {
return;
}
_speed = 0;
_pause = true;
_actualDeltaTime = _delta;
if (_count_click == 4) {
_array_results[_count_click] = getResultFromUser();
writeResults (_array_results);
_count_click = 0;
}
//Debug.Log (getResultFromUser());
_array_results[_count_click] = getResultFromUser();
}
else if (Input.GetKeyDown ("down") && _event_click){
if (_one_click > 0 ) return;
_speed = -2*Mathf.PI;
_pause = false;
_actualDeltaTime = Time.realtimeSinceStartup-_delta;
if (_count_click < 5) _count_click++;
_one_click++;
}
if (!_pause) {
_delta = _actualDeltaTime>0 ? Time.realtimeSinceStartup-_actualDeltaTime : Time.realtimeSinceStartup-_startTime;
_minutes = ((int)_delta / 60);
_seconds = (_delta % 60);
}
}
void drawCircle(float x_c, float y_c, float z_c, float radius){
LineRenderer lineRenderer;
float theta_scale = 0.01f;
float sizeValue = (2.0f * Mathf.PI) / theta_scale;
int size;
gameObject.transform.position = new Vector3(x_c, y_c, z_c);
size = (int)sizeValue;
size++;
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Diffuse"));
lineRenderer.SetWidth(0.03f, 0.03f); //thickness of line
lineRenderer.SetVertexCount(size);
Vector3 pos_dot;
float theta = 0.5f;
for(int i = 0; i < size; i++){
theta += (2.0f * Mathf.PI * theta_scale);
float x = radius * Mathf.Cos(theta);
float y = radius * Mathf.Sin(theta);
x += gameObject.transform.position.x;
y += gameObject.transform.position.y;
pos_dot = new Vector3(x, y, z_c);
lineRenderer.SetPosition(i, pos_dot);
}
GameObject ob = GameObject.Find("Sphere");
LineRenderer lineRenderer_cross1 = new GameObject().AddComponent<LineRenderer>();
lineRenderer_cross1.material.color = Color.red;
lineRenderer_cross1.SetWidth(0.03f, 0.03f);
lineRenderer_cross1.SetPosition(0, new Vector3(x_c, y_c+radius+transform.localScale.x/2, z_c));
lineRenderer_cross1.SetPosition(1, new Vector3(x_c, y_c+radius-transform.localScale.x/2, z_c));
LineRenderer lineRenderer_cross2 = new GameObject().AddComponent<LineRenderer>();
lineRenderer_cross2.material.color = Color.red;
lineRenderer_cross2.SetWidth(0.03f, 0.03f);
lineRenderer_cross2.SetPosition(0, new Vector3(x_c+transform.localScale.x/2, y_c+radius, z_c));
lineRenderer_cross2.SetPosition(1, new Vector3(x_c-transform.localScale.x/2, y_c+radius, z_c));
}
public void writeResults(float[] array_results){
FileInfo F = new FileInfo (@"C:\tmp\result_rdo.txt");
using (StreamWriter str = F.AppendText ()) {
for (int j = 0; j < 5; j++) {
str.WriteLine (array_results [j]);
}
str.WriteLine ("__________");
}
}
public float getResultFromUser(){
float res=0f;
float mod = _minutes>0 ? 60*_minutes+_seconds : _seconds;
float num = mod % period;
num = Mathf.Round(100 * num) / 100;
res = num>=period/2 ? num-period : num;
res = Mathf.Round(100 * res) / 100;
Debug.Log(res);
//Debug.Log(res);
return res;
}
}
Comment