- Home /
Vector2 Error when Declaring new Vector3
Hello,
I'm currently working on a project that uses the ScreenToWorldPoint function to develop a touch control system.
Here's the line in question:
var touchPosition : Vector3 = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, 0, touch.position.z));
I want to use "Camera.main.ScreenToWorldPoint" to create a new Vector3. However, my console returns the following error when I compile the code.
Error BCE0019: 'z' is not a member of 'UnityEngine.Vector2'. Did you mean 'x'? (BCE0019).
I think the issue is that it believes I want a 2d vector when in reality I want to create a 3d vector.
Here's the complete code for reference.
function Update () {
var object : GameObject;
for(var touch : Touch in Input.touches){
if(Input.touchCount > 0) {
// Check for Touch on Screen.
if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
var touchPosition : Vector3 = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, 0, touch.position.z));
// Create Vector from Touch Position, and "ScreenToWorldPoint" Function.
transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime);
// Smoothly Translate GameObject according to Last Frame, Touch Position, and Time.
transform.LookAt(touchPosition, Vector3.up);
// Orient GameObject according to Touch Posiition.
}
}
}
}
I'm sure there's something right in front of me that I'm missing but I've been fighting with this for a little too long so I'm turning to the community!
Thanks for any help!
Answer by Jeff-Kesselman · Nov 23, 2014 at 06:55 AM
touch is a Vector2. use 0 for the third parameter
Thanks for your reply!
That did remove my error, however, $$anonymous$$y intention was to have the object move on the XZ plane. Wouldn't declaring the z parameter 0 meanthe object would only transform on the XY plane?
Thanks again!
-Trent