I’m trying to create a function that tells an object to move to a location. I’d love to be able to pass in a start location occasionally but not in all situations. (When I don’t pass one in it would use its current location). I know how to use default parameters but I get an error when trying to do it with Vector3s no matter what I try. I have googled solutions and run into people with the same problem, but all of the accepted solutions actually don’t work for me. Am I doing something wrong or is this impossible? Is there some other workaround?
Here’s the relevant snippet of my code:
MoveToTarget(Vector3 targetLocation, float moveSpeed, Vector3 startLocation = this.gameObject.transform.localPosition))
{
This didn’t work so I tried it like this as well:
MoveToTarget(Vector3 targetLocation, float moveSpeed, Vector3 startLocation = new Vector3(1000,1000,1000))
{
if(startLocation==new Vector3(1000,1000,1000))
{
//Code to use my current location
}
I also tried it like this:
public Vector3 nullVector= new Vector3(1000,1000,1000);
MoveToTarget(Vector3 targetLocation, float moveSpeed, Vector3 startLocation = nullVector)
{
if(startLocation==nullVector)
{
//Code to use my current location
}
and like this:
MoveToTarget(Vector3 targetLocation, float moveSpeed, Vector3 startLocation = null)
{
if(startLocation==null)
{
//Code to use my current location
}
One thing I could do for sure is this, but I’d rather not have to pass in an unused vector3 every time I call the function:
MoveToTarget(Vector3 targetLocation, float moveSpeed, Vector3 startLocation, bool useCurrentLocAsStart=true)
{
if(useCurrentLocAsStart==false)
{
//Use the actual start location I passed in
}
Is there any way to accomplish what I am looking to do other than my above solution? Thanks.