Hello, I’m a complete noob but I was able to get values from arduino to unity and apply a xyz rotation(found the code online). The problem is I want to recieve more values from the arduino and apply that to the rotation of more than one object. I need to create one script for each object and I can only have the serial port in one scrip. I need to get the values from the scrip that reads the serial into the other scripts.
Here is my code:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class NewBehaviourScript : MonoBehaviour {
SerialPort stream = new SerialPort("COM3", 38400); //Set the port (com4) and the baud rate (9600, is standard on most devices)
float[] lastRot = {0,0,0}; //Need the last rotation to tell how far to spin the camera
Vector3 rot;
Vector3 offset;
void Start () {
stream.Open(); //Open the Serial Stream.
}
// Update is called once per frame
void Update () {
string value = stream.ReadLine(); //Read the information
string[] vec3 = value.Split(','); //My arduino script returns a 3 part value (IE: 12,30,18)
if(vec3[0] != "" vec3[1] != "" vec3[2] != "" vec3[3] != "" vec3[4] != "" vec3[5] != "") //Check if all values are recieved
{
rot = new Vector3(float.Parse(vec3[0]),float.Parse(vec3[1]),float.Parse(vec3[2]));
//Read the information and put it in a vector3
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.Euler(0,rot.x,rot.y),
Time.deltaTime*3);
//Take the vector3 and apply it to the object this script is applied.
stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
}
}
void OnGUI()
{
string newString = "Connected: " + transform.eulerAngles;
GUI.Label(new Rect(10,10,300,100), newString); //Display new values
GUI.Label(new Rect(10,30,300,100), "\t" + rot);
}
}
Thank you for your support! You gave me three solutions and I don’t know how to apply any of them lol
The static variable seems the most simple option, where in the code should I put it?
make another script, which has a function you will use to update the rotation of that object.
public class ObjectScript : MonoBehaviour {
private Transform selfTransform;
void Start() {
selfTransform = transform;
}
public void UpdateRotation(Vector3 _rotation) {
selfTransform.eulerAngles = _rotation;
}
}
then place this script on all your gameobjects you want to rotate and in your script, make a public Array of this specific class.
public ObjectScript[] Objects;
then simply drag drop your objects on that script which is attached to the main Camera or any other object, so your reading script will have references to those objects. and when you need to rotate a specific object just use:
I’ve been there, I did my search before posting. The thing is, I’m a complete noob with zero experience in programming, I need a practical example or else I won’t be able to do it. My main script is reading the serial port and applying a xyz rotation to an object, my new script needs to read the values from the main and apply it to a different object. It’s so simple! If a can’t do this I’m going to quit unity forever lol
EDIT: I posted before reading aronax post. I’m going to try that, thank you!
I did what you said, I have the the two objects in the hierarchy of the camera and the camera has the serial script and the objects the update rotation script.
The game executes but I have an ‘array index is out of range’ and the rotation isn’t working.
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class ReadSerial : MonoBehaviour {
SerialPort stream = new SerialPort("COM3", 38400); //Set the port (com4) and the baud rate (9600, is standard on most devices)
float[] lastRot = {0,0,0}; //Need the last rotation to tell how far to spin the camera
Vector3 rot;
Vector3 rot2;
Vector3 offset;
public ObjectScript[] Objects;
void Start () {
stream.Open(); //Open the Serial Stream.
}
// Update is called once per frame
void Update () {
string value = stream.ReadLine(); //Read the information
string[] vec3 = value.Split(','); //My arduino script returns a 3 part value (IE: 12,30,18)
if(vec3[0] != "" vec3[1] != "" vec3[2] != "" vec3[3] != "" vec3[4] != "" vec3[5] != "") //Check if all values are recieved
{
rot = new Vector3(float.Parse(vec3[0]),float.Parse(vec3[1]),float.Parse(vec3[2]));
//Read the information and put it in a vector3
rot2 = new Vector3(float.Parse(vec3[3]),float.Parse(vec3[4]),float.Parse(vec3[5]));
Objects[0].UpdateRotation(new Vector3(rot.x, rot.y, rot.z));
Objects[1].UpdateRotation(new Vector3(rot2.x, rot2.y, rot2.z));
stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
}
}
void OnGUI()
{
string newString = "Connected: " + transform.eulerAngles;
GUI.Label(new Rect(10,10,300,100), newString); //Display new values
GUI.Label(new Rect(10,30,300,100), "\t" + rot);
}
}
I don’t know if I put the things in the right place, probably I dind’t
How can you find out, using code, how many elements are in that Objects array?
I have two cubes.
This scripts is attached to both cubes
using UnityEngine;
using System.Collections;
public class UpdateRotation : MonoBehaviour {
private Transform selfTransform;
void Start() {
selfTransform = transform;
}
public void UpdateRotate(Vector3 _rotation) {
selfTransform.eulerAngles = _rotation;
}
}
The next script is attached to the camera
using UnityEngine;
using System.Collections;
public class RotateObjects : MonoBehaviour {
public GameObject[] Cubes;
private int x = 0;
private int y = 0;
private int z = 0;
private UpdateRotation UpdateRot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
x++;
y++;
z++;
for (int i = 0; i < 2; i++)
{
UpdateRot = Cubes[i].GetComponent<UpdateRotation>();
UpdateRot.UpdateRotate(new Vector3(x, y, z));
}
}
}
The cubes are assigned via the inspector to the “cubes” array, under the “rotate objects” script.
Is there are more direct way? I thought I’d be able to declare an explicit array of cubes and I wouldn’t need to use GetComponent to access its UpdateRotation. I get the feeling I’d need to create a “prefab” out of a cube with the script attached, and instantiate the cubes at runtime in order to get that functionality, but i can’t seem to get away from having to work through “GameObject” to access this stuff.
It’s working
My problem was this: “The cubes are assigned via the inspector to the “cubes” array, under the “rotate objects” script.” so thank you very much NA-RA-KU!
Glad you worked it out. “Array out of bounds” error was, I assume, caused by not setting the references to the array elements in the inspector. And one more thing, in a previous post you said that you have 2 cubes parented by the camera, if so, you can use transform.localRotation to make the cubes rotate using their axis but also keep and be influenced by it’s parent rotation (a.k.a. Camera)