I am trying to move me gameObject by getting its rotation and calculating where its next position will be according to its rotation and increment it inside a loop, its like, go forward according to where its “facing” or “pointing”.
Here is the piece of code im using to try to achieve that:
NpcPos = this.transform.position;
NpcRot = this.transform.rotation.eulerAngles;
double movQuantity = 0.2;
float anglex = (float)(NpcRot.x * (Math.PI / 180));
float angley = (float)(NpcRot.z * (Math.PI / 180));
NpcPos.x = NpcPos.x + (float)(Math.Sin((double)angley) * Math.Cos((double)anglex) * movQuantity);
NpcPos.z = NpcPos.z + (float)(Math.Cos((double)angley) * Math.Cos((double)anglex) * movQuantity);
NpcPos.y = NpcPos.y + (float)(Math.Sin((double)anglex * movQuantity));
this.transform.position = NpcPos;
Unity already does that a bit easier for us with the following code, which is exactly the functionality I want to “copy”
var moveDirection = transform.TransformDirection(Vector3.forward);
controller.Move(moveDirection * Time.deltaTime);
I was following this tutorial to do that:
http://answers.yahoo.com/question/index?qid=20080121092931AAC9flk
, It looks like in this tutorial the z and y axes are swapped…
Anyway… I know that there is another way to do that, which is by vector calculation, I think thats how unity actually does that, but I cant wrap my head around it. Ive been reading on the internet about it but its a bit hard to find as most game engines already do that part, even someone programming directly on directX or OpenGL can use its internal functions to do the vector calculation.
You may be asking by now, why the hell you want to do that if unity can do that for you.
Answer, I dont intent do use that in unity, I want to use the code in my server (photon) because when the player shoots, I want the server to controll the projectiles, which I think its how it has to be.
Im trying to get this working for the past few days and the problem is that I think the trigonometry on that tutorial must be wrong beacuse the projectile never goes straight to where it is “facing” according to its rotation.
I am desperate to solve that as I am spending too much time trying to get this working and going nowhere.
If anyone knows a better way to do that please, pretty please, let me know.
Thanks