If a Vector3 is called in multiple functions of the same class is better for performance to keep it outside the scope of the functions and have one instance rather than multiple per function? If so would it be better to have a separate class with a static function? Example would be this vector3 that is used in multiple functions. Code (CSharp): Vector3 playerPosition = new Vector3 (player.position.x, player.position.y + 0.65f, player.position.z) - muzzlePosition; If you happen to know of any good links with information in regards to best practices with this in mind i would love to read them. Thankyou Guys.
I wouldn't worry about the performance. Vector3 is a struct, and structs get a bit weird with instances anyway. What I would worry about is maintainatilibty and development speed. If the particular spot 0.65 units above the player object is important, then you want to move it out of the function and into its own member. That way when your play testers come back and say it should actually be 0.5 units instead you can change it once. Google magic numbers for a more in depth discussion on the topic.
Am I the only one who hates using the ctor to build a new vector based on another one? Code (csharp): playerPosition = player.position + Vector3.up * 0.65f - muzzlePosition;
I am just curious, why do you hate this, and how would you do it? [e] Nevermind. I thought you were talking about Code (CSharp): playerPosition = player.position + Vector3.up * 0.65f - muzzlePosition; And I couldn't figure out what's wrong with it. Now I am realizing that your statement is about the use of New() in the initial post. Sorry.
My brain would just rather do the math I guess. And it feels like a lot of redundant typing. And my post showed how I do it.
I didn't know i could go about it that way KelsoMRK since i am self taught from examples and don't yet have the knowledge for best practice. How would you approach something like this so i know for future reference. Would this be a situation that a new vector should be used? Code (CSharp): playerPosition += new Vector3 (Random.Range (-maxSpread, maxSpread) * shotSpread, Random.Range (-maxSpread, maxSpread) * shotSpread, 1); Thankyou for the example . Do you know of any good books or links in regards to best practices?
I'm not sure what you mean when you say "Best Practices" as your original question was more geared to performance and my post was about personal preference when doing a particular operation. Best Practices fall more into the category of "here's how you name your variables", "how to organize your namespaces", "how to write a for loop", "when to use var and when to explicitly declare the type", etc etc. To that end, MSDN does have some articles but admits that there is no hard-and-fast "Correct" way to do something. Not to mention the fact that Best Practices vary from company to company (and even within a company if it's large enough). What you'll find is that you'll develop a way of writing code that works for you - just like you develop a manner of speaking; or writing; or taking a shower in the morning (or evening!). If the end product performs at the same level of efficiency and is clear when you read it again in 3 years then you're well on your way to writing good code (whatever that means). I'm completely self-taught too btw
For readability on complex vectors I often do this Code (CSharp): Float x = somethingCrazy; Float y = somethingElse; Vector2 vector = new Vector2 (x,y);