So at the moment, I have my code to move a particle around the character in a spherical motion. I have a math problem somewhere because it is not doing what I am trying to get it to do.
Here is my code:
class SwordAttack : MonoBehaviour{
//this will be in equipment
float swordrange = 1;
//extra processing variables
float basecordinanty;
float swordpointz1;
float swordpointz2;
struct screen{
public float x, y;
}
struct Point{
public float x, y, z;
}
void Update()
{
//finds center of screen
screen center;
center.x = Screen.width / 2;
center.y = Screen.height / 2;
//if mouse is pressed
if (!Input.GetMouseButton (0))
return;
//sets basepoint which is "directly in middle of screen"?
Point basepoint;
//math for basepoint
float camx = Camera.main.transform.eulerAngles.x;
float camy = Camera.main.transform.eulerAngles.y;
basepoint.x = Mathf.Sin (camy);
basepoint.z = Mathf.Sin (camx);
basecordinanty = Mathf.Cos (camx) + Mathf.Cos (camy);
basepoint.y = basecordinanty / 2;
//sets swordpoint which is the exact location the sword should be at in comparison to the player
Point swordpoint;
//insane math for swordpoint
swordpoint.x = (Input.mousePosition.x / (Screen.width / 2) - center.x / (Screen.width / 2)) + basepoint.x;
swordpoint.y = (Input.mousePosition.y / (Screen.height / 2) - center.y / (Screen.height / 2)) + basepoint.y;
swordpointz1 = Mathf.Sqrt ((swordpoint.x * swordpoint.x) + (swordrange * swordrange));
swordpointz2 = Mathf.Sqrt ((swordpoint.y * swordpoint.y) + (swordrange * swordrange));
swordpoint.z = ((swordpointz1 + swordpointz2) / 2) + basepoint.z;
//always the helpful debug log
Debug.Log (" swordpoint = " + swordpoint.x + ", " + swordpoint.y + ", " + swordpoint.z);
Debug.Log ("Center of the screen is " + basepoint.x + ", " + basepoint.y + ", " + basepoint.z);
Debug.Log ("Camera is at " + Camera.main.transform.position.x + ", " + Camera.main.transform.position.y + ", " + Camera.main.transform.position.z);
//finally, the code that transforms the little demon particle itself
transform.position = new Vector3(swordpoint.x + Camera.main.transform.position.x, swordpoint.y + Camera.main.transform.position.y, swordpoint.z + Camera.main.transform.position.z );
//doesnt work yet...
if(Input.GetMouseButton(0))
return;
transform.position = new Vector3(0 + Camera.main.transform.position.x,0 + Camera.main.transform.position.y,0 + Camera.main.transform.position.z);
}
void start(){
}
}
I cannot tell exactly where the code issue is. This code is attached to a particle in the unity editor which is then the child of a first person controller. Any help would be greatly appreciated.
-Z
↧