-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball_Script(With voice).cs
90 lines (70 loc) · 2.09 KB
/
Ball_Script(With voice).cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Windows.Speech;
using System;
using System.Linq;
public class Ball_Script : MonoBehaviour
{
public float minX, maxX;
public float speed;
public float horizontalSpeed;
public float SpeechX;
private Rigidbody myBody;
private bool thrown = false;
private Dictionary<string, Action> keywords = new Dictionary<string, Action>();
private KeywordRecognizer keywordRecognizer;
// Start is called before the first frame update
void Start()
{
myBody = GetComponent<Rigidbody>();
keywords.Add("left", Left);
keywords.Add("right", Right);
keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());
keywordRecognizer.OnPhraseRecognized += RecognizedSpeech;
keywordRecognizer.Start();
}
// Update is called once per frame
void Update()
{
BallMovement();
}
void BallMovement()
{
if (!thrown) // if not throw the ball yet
{
float xAxis = Input.GetAxis("Horizontal");
Vector3 position = transform.localPosition;
position.x += xAxis * horizontalSpeed;
position.x = Mathf.Clamp(position.x, minX, maxX);
transform.localPosition = position;
}
if (!thrown && Input.GetKeyDown(KeyCode.Space))
{
thrown = true;
myBody.isKinematic = false;
myBody.velocity = new Vector3(0, 0, speed);
}
}
private void FixedUpdate() //need go build setting add scene
{
if(thrown && myBody.IsSleeping())
{
SceneManager.LoadScene("GameScene");
}
}
private void RecognizedSpeech(PhraseRecognizedEventArgs speech)
{
Debug.Log(speech.text);
keywords[speech.text].Invoke();
}
private void Left()
{
transform.Translate(SpeechX, 0, 0);
}
private void Right()
{
transform.Translate(-SpeechX, 0, 0);
}
}