- Home /
The question is answered, right answer was accepted
Transform Position
Hello. For a school project we had to create our own Vector 3 In unity and use it later to move objects. The goal was to make a sphere move to the position of the cube using our own vector and the subtraction of vectors (SubtractVector Function). But every time I try to set the destination of Sphere to the one I calculated on the SubtractVector function it shows me an error of "Cannot implicity convert type "MyVector3" to "UnityEngine.Vector3". Can someone give me some help..?
My code:
public GameObject Cube;
public GameObject Sphere;
private void Start()
{
Sphere = GameObject.Find("Sphere");
}
private void Update()
{
Vector3 test = new Vector3(1, 1, 1).normalized;
if (Input.GetKeyDown("space"))
{
MyVector3 cubePos = new MyVector3(Cube.transform.position.x, Cube.transform.position.y, Cube.transform.position.z);
MyVector3 spherePos = new MyVector3(Sphere.transform.position.x, Sphere.transform.position.y, Sphere.transform.position.z);
MyVector3 move = MyVector3.SubtractVector(spherePos, cubePos);
Sphere.transform.position = move;
}
}
}
Own Vector code:
public float x,y,z;
public MyVector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static MyVector3 SubtractVector(MyVector3 vec1, MyVector3 vec2)
{
MyVector3 rv = new MyVector3(0, 0, 0);
rv.x = vec1.x - vec2.x;
rv.y = vec1.y - vec2.y;
rv.z = vec1.z - vec2.z;
return rv;
}
Thank you.
Answer by rh_galaxy · Mar 24 at 03:10 PM
You need to create the Vector3 and Init it yourself, it can't be automatic.
Sphere.transform.position = new Vector3(move.x, move.y, move.z);
Follow this Question
Related Questions
Get an Object to move to a Position for a Fixed time 2 Answers
How to reverse a sin animation? 2 Answers
Confusion in deciding the units for game objects 1 Answer
transform.position on Instaniated object does not match inspector 0 Answers
Transform.position.x new vector3 NOT changing object position 1 Answer