I need a quick fix for returning objects to localPositions

So I have an object “Centerax” which moves on its local X-axis, however I need it to be always trying to return to its original position relative to its parent object. (The parent object is always moving/rotating)

Currently my code below is giving me the errors listed and nothing I’ve tried seems to fix it.

public Transform target;
public float upDistance = 7.0f;
public float backDistance = 10.0f;
public float trackingSpeed = 3.0f;
public float rotationSpeed = 9.0f;
public float returnSpeed = 3.0f;

public HoverControl hcs;

private Vector3 vPos;
private Vector3 oPos;
private Quaternion rPos;

void Start ()
{
oPos = target.localPosition;
}

void FixedUpdate ()
{
float mvSlide = -Input.GetAxis(“Horizontal”) * (hcs.agility * 2);

vPos = target.position - target.forward * backDistance + target.up * upDistance;
transform.position = Vector3.Lerp (transform.position, vPos, trackingSpeed * Time.deltaTime * hcs.velocity);
rPos = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, rPos, rotationSpeed * Time.deltaTime * (hcs.agility * 2));

hcs.centerax.transform.Translate(new Vector3(Mathf.Lerp(-mvSlide, mvSlide, Time.smoothDeltaTime), 0, 0));

Vector3 tmpPos = hcs.centerax.transform.localPosition;
tmpPos.x = Mathf.Clamp (tmpPos.x, -1f, 1f);
hcs.centerax.transform.localPosition = tmpPos;

hcs.centerax.transform.localPosition = Vector3.Lerp (hcs.centerax.transform.localPosition.x, oPos, Time.deltaTime * returnSpeed);
}
}

Errors:
-The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)’ has some invalid arguments

  • Cannot convert ‘Float’ expression to type ‘UnityEngine.Vector3’

Since you didn’t tell us what line that error is reported on, I’d have to read through the whole thing and look for the error. A quick scan didn’t make it jump out at me.

However, what did jump out at me is that you’re misusing Lerp in every case. Any time you find yourself writing something of the form of “foo = Lerp(foo, target, delta)”, you are abusing Lerp. You should be using MoveTowards instead (which is defined in Mathf, Vector3, and Quaternion, just like Lerp).

So, fix that first, and if you’re still getting the error, post again using code tags and reporting which line the error is on.

Thanks for the quick reply Joe, I didn’t realise ‘MoveTowards’ was so similar so I’ll read up on that link about Lerping thanks mate.

But the problem line is (39,64) for both errors, it looks like this now:

hcs.centerax.transform.localPosition = Vector3.MoveTowards (hcs.centerax.transform.localPosition.x, oPos, Time.deltaTime * returnSpeed);

I think its the ‘oPos’ I set during the start of the script, but in my eyes I don’t see whats wrong. :confused:

Ah, the problem is the first parameter — it’s supposed to be a Vector3, but you’ve specified hcs.centerax.transform.localPosition**.x**, which is a float. Take off the .x and you should be good to go!

Ah its always the little things isn’t it.
Everything works as intended now cheers.