Home > other >  The Unity of the script Vector3, Camera, Input does not change color
The Unity of the script Vector3, Camera, Input does not change color

Time:03-20

using System.Collections;
Using System. Collections. Generic;
Using UnityEngine;

Public class Bird: MonoBehaviour
{
Private bool isClick=false;

Private void onm ouseDown ()//mouse press
{
IsClick=true;
}

Private void onm ouseUp ()//mouse lift
{
IsClick=false;
}

//Start is called before the first frame update
Void the Start ()
{

}

//Update is called once per frame
Void the Update ()
{
If (isClick)//mouse has been pressed, for the location of the men with
{
The transform. The position=Camera. The main SceenToWorldPoint (Input. MousePosition);
The transform. The position +=new Vector3 (0, 0, 10);
}
}
}

A problem with the code and run wrong

CodePudding user response:

There is a problem
1, determine the mouse press don't need to bother, then the Update call directly Input. GetMouseButton (0),
. 2, the Camera. The main ScreenToWorldPoint, not so easy to use, screen dot not directly into the world coordinate, because the screen is 2 d, in theory, turn to the world coordinate is a line, rather than a point, you should use the X-ray testing, use a starting from the Camera, through the mouse point ray, to detect encountered objects (such as ground), based on the collision point, to execute your code, the displacement