Im a Unity noob, i only know how to change the player position with KeyCode A or KeyCode D, how to change it to use the x-axis of my face(OpenCV plus) to control the direction of my player?
Project Link:https://drive.google.com/file/d/1oKrhCWD0nVVz_-4YfimIw0Bg7CnEAJcQ/view?usp=sharing
- FaceDector.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OpenCvSharp;
public class FaceDector : MonoBehaviour
{
// Start is called before the first frame update
WebCamTexture _webCamTexture;
CascadeClassifier cascade;
OpenCvSharp.Rect MyFace;
public float faceX;
void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
_webCamTexture = new WebCamTexture(devices[0].name);
_webCamTexture.Play();
cascade = new CascadeClassifier("Assets/haarcascade_frontalface_default.xml");
}
// Update is called once per frame
void Update()
{
GetComponent<Renderer>().material.mainTexture = _webCamTexture;
Mat frame = OpenCvSharp.Unity.TextureToMat(_webCamTexture);
findNewFace(frame);
}
void findNewFace(Mat frame)
{
var faces = cascade.DetectMultiScale(frame, 1.1, 2, HaarDetectionType.ScaleImage);
if (faces.Length >= 1)
{
Debug.Log(faces[0].Location);
MyFace = faces[0];
faceX = faces[0].X;
}
}
void display(Mat frame)
{
if (MyFace != null)
{
frame.Rectangle(MyFace, new Scalar(250, 0, 0), 2);
}
Texture newtexture = OpenCvSharp.Unity.MatToTexture(frame);
GetComponent<Renderer>().material.mainTexture = newtexture;
}
}
- Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
FaceDector faceDector;
float lastX = 0;
[SerializeField] float moveSpeed = 5f;
[SerializeField] int Hp;
[SerializeField] GameObject HpBar;
[SerializeField] GameObject replaybutton;
[SerializeField] GameObject Playerlook;
[SerializeField] Text scoreText;
[SerializeField] Text SecondText;
[SerializeField] int score;
[SerializeField] Text goalText;
float goal;
float Second;
int SecondTime;
// Start is called before the first frame update
public void Start()
{
Hp = 10;
score = 0;
Second = 0;
SecondTime = 0;
goal = 20;
faceDector = (FaceDector)FindObjectOfType(typeof(FaceDector));
}
// Update is called once per frame
public void Update()
{
float step = moveSpeed * Time.deltaTime;
float norm = Mathf.Clamp(1, faceDector.faceX - lastX, 1);
transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x + norm, transform.position.y, transform.position.z), step);
if(Input.GetKey(KeyCode.A))
{
transform.Translate( -moveSpeed*Time.deltaTime, 0,0);
GetComponent<SpriteRenderer>().flipX=true;
}
else if(Input.GetKey(KeyCode.D))
{
transform.Translate( moveSpeed*Time.deltaTime, 0,0);
GetComponent<SpriteRenderer>().flipX=false;
}
UpdateSecond();
}
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Floor2")
{
ModidyHp(-1);
}
else if(other.gameObject.tag == "Floor1")
{
ModidyHp(2);
UpdateScore();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Deatharea")
{
ModidyHp(-10);
Die();
}
}
void ModidyHp(int num)
{
Hp += num;
if(Hp>10)
{
Hp = 10;
}
else if(Hp<0)
{
Hp = 0;
Die();
}
UpdateHpBar();
}
void UpdateHpBar()
{
for(int i=0; i<HpBar.transform.childCount; i++)
{
if(Hp>i)
{
HpBar.transform.GetChild(i).gameObject.SetActive(true);
}
else
{
HpBar.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
void UpdateScore()
{
score++;
scoreText.text = score.ToString();
}
void UpdateGoal(int num)
{
goal += num;
goalText.text = goal.ToString();
}
void UpdateSecond()
{
Second += Time.deltaTime;
if(Second>1f)
{
SecondTime++;
Second = 0f;
SecondText.text = SecondTime.ToString();
}
else if(SecondTime>60f && score<goal || Hp<1f)
{
Die();
}
else if(SecondTime>60f && score>goal && Hp>0f)
{
SecondTime = 0;
UpdateGoal(+30);
}
else if(score>goal)
{
SecondTime = 0;
UpdateGoal(+30);
}
}
void Die()
{
Time.timeScale = 1f;
replaybutton.SetActive(true);
Playerlook.SetActive(false);
}
public void Replay()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Scene1");
}
}