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
First off, I have to put this in the answer section because this post exceeds the character for a comment.
I have created this new code. The only reason I am still using the struct is because it is easy for me to think of it as I am coding. I WILL be taking those out and changing those to Vector3 when I finish to make more efficient.
Ok, clarification and updates here. What it is, is I now have an object (player) with a particle attached to it. This particle system is reduced down so it appears to be a single particle. Then I want this code to make it so that the particle is theoretically bound to a sphere around the player. First, the coordinate of the particle is based off of a point, 1 unit from the center of the camera. Then I have code to calculate the particle to the mouse position and the anywhere the mouse moves. I do this second part by using Pythagorean theorem, the distance from the middle of the screen, to the location of mouse, and then using 1 as the hypotenuse (because radius is 1) to find the location of the z point.
Here is the code I have so far:
using UnityEngine;
using System;
[AddComponentMenu("Player/Sword Attack")]
class SwordAttack : MonoBehaviour{
public float SwordLineRatio = 2;
//this will be in equipment
float swordrange = 1;
//extra processing variables
float basecordinanty;
float swordpointz1;
float swordpointz2;
//path of sword
int[] swordpath;
//counts the number in the array
int arraynum;
struct screen{
public float x, y;
}
public GameObject Sword_Line;
struct Point{
public float x, y, z;
}
void Update()
{
//if mouse is pressed
if (!Input.GetMouseButton (0))
return;
//sets basepoint which is "directly in middle of screen"?
Point basepoint;
//Setting Basepoint
basepoint.x = Vector3.forward.x + Camera.main.transform.position.x;
basepoint.y = Vector3.forward.y + Camera.main.transform.position.y;
basepoint.z = Vector3.forward.z + Camera.main.transform.position.z;
//sets swordpoint which is the exact location the sword should be at in comparison to the player
Point swordpoint;
//sets pre swordpoint
Point preswordpoint;
//math for pre swordpoint
preswordpoint.x = Input.mousePosition.x / Screen.width;
preswordpoint.y = Input.mousePosition.y / Screen.height;
//simplified math for swordpoint
Vector3 swordline = camera.ViewportToWorldPoint(new Vector3(preswordpoint.x, preswordpoint.y, camera.nearClipPlane));
swordpoint.x = swordline.x;
swordpoint.y = swordline.y;
//crazy math for swordpoint.z
Point swordpointz;
swordpointz.x = -(Input.mousePosition.x / (Screen.width * 2)) + 1;
swordpointz.y = -(Input.mousePosition.y / (Screen.height * 2)) + 1;
swordpointz1 = Mathf.Sqrt((swordpointz.x * swordpointz.x) + 1) ;
swordpointz2 = Mathf.Sqrt((swordpointz.y * swordpointz.y) + 1) ;
swordpoint.z = (swordpointz1 + swordpointz2) / 2;
//always the helpful debug log
Debug.Log (" swordpoint = " + swordpoint.x + ", " + swordpoint.y + ", " + swordpoint.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
Sword_Line.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... (will look up later)
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(){
Sword_Line = GameObject.Find("SwordLine");
}
}