Home > other >  The unity of how to implement a toward the mouse position to launch a script of a rigid body
The unity of how to implement a toward the mouse position to launch a script of a rigid body

Time:09-27

To design a similar to the artillery
Idea is shells in the gun barrel mouth first, and then let the shell to produce a force in the direction of the point just now,
Main problems in, don't know how to put the location of the mouse messages to the point when there is no shells

CodePudding user response:

EventSystem components can give you the mouse position and release information such as the next point

CodePudding user response:

Train of thought to solve the problem are as follows:
First of all, don't need to know shells didn't appear, just need to know you want to the starting position of the shells:
2. The location of the gun barrel: gun. The transform. The position

The location of the mouse access: Input mousePosition (screen 2 d coordinates)

"The mouse position information to the point when there is no shell body", this process is actually the default shell if fired from the starting position to the target position (the cursor position) process:
The function Update () {
//detection of mouse
{if (Input. GetButtonDown ())
Ray mouseray=Camera. Main. ScreenPointToRay (Input. MousePosition);//generated starting point is the camera with the mouse click on the location of the ray
//Instantiate (to generate a prefabricated object, the location of the generated, to generate the object's rotation Angle)
Gameobject bullet=Instantiate (bulletPrefab, gnu. The transform. The position, Quaternion. Identity);
Bullet. Rigidbody. AddForce (mouseRay. * the force direction);//to shell a force in the direction of the mouse
}
}



More than hope that useful to you

CodePudding user response:

refer to the second floor zjiadaxiaojie response:
ideas to solve the problem are as follows:
First of all, don't need to know shells didn't appear, just need to know you want to the starting position of the shells:
2. The location of the gun barrel: gun. The transform. The position

The location of the mouse access: Input mousePosition (screen 2 d coordinates)

"The mouse position information to the point when there is no shell body", this process is actually the default shell if fired from the starting position to the target position (the cursor position) process:
The function Update () {
//detection of mouse
{if (Input. GetButtonDown ())
Ray mouseray=Camera. Main. ScreenPointToRay (Input. MousePosition);//generated starting point is the camera with the mouse click on the location of the ray
//Instantiate (to generate a prefabricated object, the location of the generated, to generate the object's rotation Angle)
Gameobject bullet=Instantiate (bulletPrefab, gnu. The transform. The position, Quaternion. Identity);
Bullet. Rigidbody. AddForce (mouseRay. * the force direction);//to shell a force in the direction of the mouse
}
}



More than hope that useful to you

That place this script should be who is more appropriate?

CodePudding user response:

reference m0_46201414 reply: 3/f
Quote: refer to the second floor zjiadaxiaojie response:
ideas to solve the problem are as follows:
First of all, don't need to know shells didn't appear, just need to know you want to the starting position of the shells:
2. The location of the gun barrel: gun. The transform. The position

The location of the mouse access: Input mousePosition (screen 2 d coordinates)

"The mouse position information to the point when there is no shell body", this process is actually the default shell if fired from the starting position to the target position (the cursor position) process:
The function Update () {
//detection of mouse
{if (Input. GetButtonDown ())
Ray mouseray=Camera. Main. ScreenPointToRay (Input. MousePosition);//generated starting point is the camera with the mouse click on the location of the ray
//Instantiate (to generate a prefabricated object, the location of the generated, to generate the object's rotation Angle)
Gameobject bullet=Instantiate (bulletPrefab, gnu. The transform. The position, Quaternion. Identity);
Bullet. Rigidbody. AddForce (mouseRay. * the force direction);//to shell a force in the direction of the mouse
}
}



More than hope that useful to you

That place this script should be who is more appropriate?

On Camera or other has always been the inside of the Scene can be any object, the normal go on Camera
  • Related