Hey!
Im searching for a solution, how to save a specific number for each vector3. I want to stick them together to find out the number via debug or sth like this.
I hope there is someone able to help me
Cheers
Hey!
Im searching for a solution, how to save a specific number for each vector3. I want to stick them together to find out the number via debug or sth like this.
I hope there is someone able to help me
Cheers
Like this?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VectorIdentificator : MonoBehaviour
{
//fill this in the editor
public List<VectorID> list;
private void Start()
{
Debug.Log("All content:");
ShowFullContent();
Debug.Log("__");
Debug.Log("Only with ID = 1");
ShowByID(1);
}
public void ShowFullContent ()
{
foreach(VectorID current in list)
{
Debug.Log(current.id);
Debug.Log(current.vector);
}
}
public void ShowByID (int i)
{
VectorID temp = FindByID(i);
if(temp != null)
{
Debug.Log(temp.id);
Debug.Log(temp.vector);
}else{
Debug.Log("There is no Vector with id = "+i);
}
}
public VectorID FindByID(int id)
{
foreach(VectorID current in list)
{
if(current.id == id)
{
return current;
}
}
return null;
}
}
[System.Serializable]
public class VectorID
{
public int id;
public Vector3 vector;
}
Sounds like a dictionary. If you don’t have duplicate Vector3s you can just do something like
using UnityEngine;
using System.Collections.Generic;
public class NewBehaviourScript : MonoBehaviour {
Dictionary<Vector3, int> dictionary=new Dictionary<Vector3, int>();
void Start(){
dictionary.Add(Vector3.down, 0);
dictionary.Add(Vector3.up, 1);
dictionary.Add(Vector3.left, 2);
dictionary.Add(Vector3.right, 3);
Debug.Log("Right: "+dictionary[Vector3.right]);
Debug.Log("Down: "+dictionary[Vector3.down]);
Debug.Log("Left: "+dictionary[Vector3.left]);
Debug.Log("Up: "+dictionary[Vector3.up]);
}
}
If you do have duplicate Vector3s then it’s a little bit tricker and you’ll have to do some more work
I’m not quite sure i understand your question
If you want to save each components in a variable, just make a public variable on top of the start, and update function and later on equal that variable to vector3.x , vector3.y , vector3z …
if you want to obtain a magnitude of the vector just type in vector3.magnitude which returns the length of the vector3
actually a vector3 is made of three float’s. not int’s. i would use floats variables for your variables to avoid errors. and this is your javascript.
var yourvector:Vector3;
var spotx:float;
var spoty:float;
var spotz:float;
yourvector=Vector3(10,20,30);
spotx=yourvector.x;
spoty=yourvector.y;
spotz=yourvector.z;