Error I got is: error CS0029: Cannot implicitly convert type UnityEngine.Vector2' to UnityEngine.Transform
The line it’s reffering to is: Transform arriveAt = new Vector2(rotVec.x, rotVec.y);
Tried to fix it with:
Transform arriveAt = new (Transform)Vector2(rotVec.x, rotVec.y);
Ended up with
“Unexpected symbol (”
Transform arriveAt = (Transform)Vector2(rotVec.x, rotVec.y);
Ended up with error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected
Well, a Transform is not a Vector2. Don’t worry, I can get you on the right track. Try this.
Vector2 arriveAt = new Vector2(rotVec.x, rotVec.y);
Transform myTransform = //Get a reference to the transform you want to modify here
myTransform.position = arriveAt;
The issue is that you are trying to assign a Vector2 to a Transform variable. These are two different types and they are not interchangeable. What you want to do is get a reference to the transform you are trying to modify and them modify the parameters accordingly e.g. rotation, position, localScale, etc.
Hope this steers your in the right direction! Let me know if you need further clarification. Posting your code would be helpful.
I see what you’re attempting, which is a great way to solve it.
But “arriveAt” is used in “Vector2.Distance” and it doesn’t accept Vector2 but transform.position. arriveAt has to be Vector2 for movement with lerp (I’ll do lerp movement myself) and transform.position to compare the distances. That’s why it was meant to be Transform in the first place. Because Transform can be easily converted to Vector2.
A Transform CANNOT be converted to a Vector2. It’s impossible. The position of a Transform can be converted to a Vector2, though it is actually a Vector3, but the Transform itself cannot. Without seeing your implementation it will be difficult to solve your specific problem. What exactly are you trying to do with this Transform?
Vector2.Distance absolutely does accept a Vector2…it requires two of them and returns a float. Usage is like this:
float dist = Vector2.Distance(V1, V2);
transform.position is also a Vector2; not a transform.
Don’t worry, you’ll get the hang of this! Just stick with it and don’t get discouraged. Learning to write code is not easy.
If you say that arriveAt has to be a Vector2, why do you declare it to be a Transform?
Because it would need to be transform for distance check.
Learning to write code is not easy. , I’m actually C#/Java/PHP programmer, I’m just not used to your API, such things as “Transform” and “Vector2” are not used in VC+ or Qt or anything I’ve seen before.
float dist = Vector2.Distance(V1, V2);
But then, public GameObject Player; would need to be converted to Vector2. But I somehow try to manage the code, I got the Vector of player, applied it to distance check, but then there’s another error, involved code:
Vector2 vec = new Vector2(Player.transform.position.x + KeepDistance, Player.transform.position.y);
// Get the desired distance.
Quaternion rot = Quaternion.Euler(0, 0, HardToFollow);
// Get the desired angle.
Vector2 rotVec = rot * vec;
// Get Vector of the position
transform.position = new Vector2(rotVec.x, rotVec.y);
// Attempt movement to the Vector2.
Vector2 arriveAt = Vector2(rotVec.x, rotVec.y);
// The targetting of the place where you want to go.
The error: error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected.
But then again, I could also make a new Vector2() out of it.
Dude, Transform has it’s own properties, one of which is a Vector3 called .position. Check the docs: Unity - Scripting API: Transform and it will become clear. Start with docs next time
I do suggest checking the docs before posting a question on here. I wasn’t trying to demean your abilities but the way you were phrasing things lead me to believe that you were a novice programmer with no experience in C# or Unity.
When you have to be told that a Transform is not a Vector2 it does indicate that you have not read the docs and that you are a novice programmer. The sky is not the ocean; it’s the sky. A dog is not a cat; it’s a dog. A Transform is not a Vector2; it’s a Transform. C# has strict standards and things often have to be declared explicitly because no implicit conversion exists. I mean no offence by this of course; I’m just providing some hyperbolic simile. If you are more comfortable with Java, Unity does accept Javascript as well which is a little looser with standards. Though, you still can’t call a Transform a Vector2 and make it so.
That’s entirely wrong. The distance check takes Vector2 parameters, not Transform parameters. Moreover, a GameObject is not a Vector2 either, but gameObject.transform.position is.
I suggest you take some time to familiarize yourself with the API