Question by
RedMutineer · Mar 17, 2018 at 02:16 PM ·
unity 5arduinoserialportwrite data
Hi I am trying to write some data to an Arduino via COM port when my player is hit by a raycast, as simple as possible. Any Help or Code for this would be great thanks ??
I am building a very basic VR fps game but need to write some data to an Ardunio via COM port. I want to turn an led on when the raycast hits a game object in the game. Can anyone provide some basic code that will do this, it only needs to be simple. Any help would be greatly appreciated, I am happy to share the project with anyone that can help solve this problem. Thanks.
I have provided a script below that is attached to an enemy to fire a raycast out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class EnemyScriptTest002 : MonoBehaviour {
public Transform player;
public float range = 50.0f;
public float bulletImpulse= 20.0f;
public Transform muzzleTransform;
private bool onRange= false;
//public Rigidbody projectile;
void Start(){
float rand = Random.Range (1.0f, 2.0f);
InvokeRepeating("Shoot", 2, rand);
}
void Update(){
onRange = Vector3.Distance(transform.position, player.position)<range;
if (onRange)
transform.LookAt(player);
}
void Shoot ()
{
if (onRange) {
RaycastHit hit = new RaycastHit ();
Ray ray = new Ray (muzzleTransform.position, muzzleTransform.TransformDirection (Vector3.forward));
Debug.DrawRay (transform.position, transform.forward, Color.green);
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.CompareTag ("PlayerTransform")) {
Debug.Log ("We've Hit " + hit.collider.gameObject.name);
// need to add arduino output here.
}
}
}
}
}
Comment