0
\$\begingroup\$

I'm trying to make a respawn point for my car to jump back to if it presses a button. I can see, using the Print() lines, that all the variables are correct and the methods are being triggered when expected.

But the car doesn't go back to the spawnpoint like I want it to. The velocity part seems to take effect and work ok, but the rotation and position part does nothing.

I tried several different variations of this code (ie. GetComponent<> and setting a public player variable (even though I am already in the player with this script)).

Like I said I tried making a public Player and setting the transform p and r on that. I also tried GetComponentByParent<Transform> and setting like that. I've tried a few other variations of that too. And I get the same results.

Here is the bit of code I think is relevant, if you actually need to see more please let me know.

public void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "RespawnTrack")
    {
        print("respawn point added upon Trigger Exit");
        recordedTransformAtRespawnTrack = transform;
        recordedVelocityAtRespawnTrack = rb.velocity * 0.25f;
        print("rotation was: X,Y,Z=" + recordedTransformAtRespawnTrack.rotation.x + " , " + recordedTransformAtRespawnTrack.rotation.y + " , " + recordedTransformAtRespawnTrack.rotation.z);
        print("position was: X,Y,Z=" + recordedTransformAtRespawnTrack.position.x + " , " + recordedTransformAtRespawnTrack.position.y + " , " + recordedTransformAtRespawnTrack.position.z);
    }
}

public void CheckToRespawnCar()
{
    if (isRacing)
    {

        if (CrossPlatformInputManager.GetButtonDown("Jump"))
        {
            print("RESPAWN TRIGGERED BY PRESSING BUTTON");

            transform.position = recordedTransformAtRespawnTrack.position;
            transform.rotation = recordedTransformAtRespawnTrack.rotation;

            rb.velocity = recordedVelocityAtRespawnTrack;

        }

    }
}
\$\endgroup\$
1
\$\begingroup\$

What you are doing is making a reference to the transform object rather than a copy of it's values.

So when you copy the values later you're actually copying the current values as recordedTransformAtRespawnTrack points at transform.

C# makes a distinction between class (referenced objects) and struct (by-value objects).

Transform is a class. Which are referred to (using "pointer"). a=b; creates a copy of the reference (the "pointer") to the object.

Vector3 and Quaternion are structs. Which are copied. a=b; copies the values of the object.

This:

        transform.position = recordedTransformAtRespawnTrack.position;
        transform.rotation = recordedTransformAtRespawnTrack.rotation;

Is the same as this:

        transform.position = transform.position;
        transform.rotation = transform.rotation;

You need to save position and rotation themselves:

Vector3 savedPosition = Vector3.zero;
Quaternion savedRotation = Quaternion.identity;

public void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "RespawnTrack")
    {
        print("respawn point added upon Trigger Exit");
        savedPosition = transform.position;
        savedRotation = transform.rotation;
        recordedVelocityAtRespawnTrack = rb.velocity * 0.25f;
        print("rotation was: X,Y,Z=" + recordedTransformAtRespawnTrack.rotation.x + " , " + recordedTransformAtRespawnTrack.rotation.y + " , " + recordedTransformAtRespawnTrack.rotation.z);
        print("position was: X,Y,Z=" + recordedTransformAtRespawnTrack.position.x + " , " + recordedTransformAtRespawnTrack.position.y + " , " + recordedTransformAtRespawnTrack.position.z);
    }
}

public void CheckToRespawnCar()
{
    if (isRacing)
    {

        if (CrossPlatformInputManager.GetButtonDown("Jump"))
        {
            print("RESPAWN TRIGGERED BY PRESSING BUTTON");

            transform.position = savedPosition;
            transform.rotation = savedRotation;

            rb.velocity = recordedVelocityAtRespawnTrack;

        }

    }
}
\$\endgroup\$
  • \$\begingroup\$ thank you so much for this. I had a sneaking feeling it was something about this. Its getting a little late now so Ive bookmarked this page to read tomorrow when I back at my computer. Thanks again Im sure your detailed answer will have me up n running in no time ! \$\endgroup\$ – Big T Larrity Jul 21 '17 at 0:33
  • \$\begingroup\$ Thank you. I have put that into place and will try to remember what you have taught me about how to tell when i need to use this kind of code to copy the values. \$\endgroup\$ – Big T Larrity Jul 21 '17 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.