Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting Character Position in Unity is Not Working

I'm working on a sports game where if a player skates into the goalie crease, I want all the player positions to reset to the center of the ice and do a three second countdown before play resumes.

I have tried to hardcode the starting position for the main player in a variable called PlayerStart and I call Player.transform.position = PlayerStart. When I did this, the player didn't move so I tried to switch the object I was setting as the player. This did what I wanted, but the mouse functionality changes for some reason and when the countdown ends, the player just goes right back to the position they were in before the crease violation was called.

Other things I've tried:

  • transform.SetPositionAndRotation
  • PlayerStart = Player.transform.position (instead of hard coding the numbers in)

Here is my code:

public class Crease : MonoBehaviour
{
    private float Delay = 0;
    private bool CreaseViolated = false;
    private GameObject Player;
    public Vector3 PlayerStart;

    // Start is called before the first frame update
    void Start()
    {
        Ring = GameObject.Find("Ring");
        Player = GameObject.Find("FPSController");
        PlayerStart = new Vector3(29.75f, 6.03999996f, 4.42000008f);
    }

    // Update is called once per frame
    void Update()
    {
        Delay -= Time.deltaTime;
        if (CreaseViolated)
        {
            CreaseViolation();
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name != "Ring" 
            && other.gameObject.name != "TeamGoalie"
            && other.gameObject.name != "OpponentGoalie") 
        {
            CreaseViolated = true;
            Delay = 3;
        }
    }

    void CreaseViolation()
    {
        if (Delay > 0)
        {
            PlayerTip.GetComponent<PickupRing>().HasRing = false;
            Opponent.GetComponent<AI>().HasRing = false;
            Ring.transform.parent = null;
        }
        else
        {
            text.text = " ";
            if (CreaseViolated)
            {
                Debug.Log("Player position before: " + Player.transform.position);
                Player.transform.position = PlayerStart;
                Debug.Log("Player position after: " + Player.transform.position);
                //Player.transform.SetPositionAndRotation(PlayerStart + new Vector3(0f, 0.800000012f, 0f), new Quaternion(0f, -0.707106829f, 0f, 0.707106829f) + new Quaternion(0f, 0f, 0f, 1f));
                GameObject.Find("Countdown").GetComponent<CountdownText>().timeRemaining = 4;
                CreaseViolated = false;
            }
        }
    }
}

Here is a short YouTube video showing my code and the demo: https://www.youtube.com/watch?v=mZt_4AppBh8

like image 739
Jenna McDonnell Avatar asked Oct 28 '25 10:10

Jenna McDonnell


2 Answers

this problem is all solved now thanks to an awesome person at my university helping me out! The solution was disabling the CharacterController before repositioning the player and then enabling it again after.

So this:

Player.transform.position = PlayerStart;

in the CreaseViolation function becomes

cc.enabled = false;
PlayerController.transform.position = PlayerControllerStart;
cc.enabled = true;

with cc being declared earlier as

private CharacterController cc;

and in the start function I assigned it with the value

cc = Player.GetComponent<CharacterController>();

with PlayerController being set to the FPSController. I renamed Player to PlayerController for more clarity.

Hopefully this helps anyone having the same problem I was having!

like image 61
Jenna McDonnell Avatar answered Oct 30 '25 11:10

Jenna McDonnell


The Above answer works perfectly but might bring some issues when doing networked changes in player transform positions.

The reason the transform change doesn't work without disabling the Character Controller is because the character controller overrides the transforms.

To Disable this and enable an Auto Synchronization between transforms GoTo:

Edit > Project Settings > Physics > (Enable Auto Sync Transforms)

like image 22
Eric Mwenda Avatar answered Oct 30 '25 11:10

Eric Mwenda