简体   繁体   中英

transform.position not working in Unity 3d

I'm really new to Unity 3d and I'm trying to make a respawn with my character. It seems that the answer is really easy but I cannot see why my code is not working. If this is a duplicate, let me know.

public Vector3 PointSpawn;

void Start()
{
    PointSpawn = gameObject.transform.position;
}

void Update()
{
    if (gameObject.transform.position.y < 10)
    {
       gameObject.transform.position = PointSpawn;   // This doesn't work

       // gameObject.transform.LookAt(PointSpawn);   ---> This DOES work ok
    }
}

Parallel Script

public float HorizontalMove;
public float VerticalMove;
private Vector3 playerInput;

public CharacterController player;

public float MoveSpeed;
private Vector3 movePlayer;
public float gravity = 9.8f;
public float fallVelocity;
public float JumpForce;
public bool DoubleJump = false;

public Camera mainCamera;
private Vector3 camForward;
private Vector3 camRight;

void Start()
{
    player = GetComponent<CharacterController>();
}

void Update()
{
    HorizontalMove = Input.GetAxis("Horizontal");
    VerticalMove = Input.GetAxis("Vertical");

    playerInput = new Vector3(HorizontalMove, 0, VerticalMove);
    playerInput = Vector3.ClampMagnitude(playerInput, 1);

    CamDirection();

    movePlayer = playerInput.x * camRight + playerInput.z * camForward;

    movePlayer = movePlayer * MoveSpeed;

    player.transform.LookAt(player.transform.position + movePlayer);

    setGravity();

    PlayerSkills();

    player.Move(movePlayer * Time.deltaTime );
}

void CamDirection()
{
    camForward = mainCamera.transform.forward;
    camRight = mainCamera.transform.right;

    camForward.y = 0;
    camRight.y = 0;

    camForward = camForward.normalized;
    camRight = camRight.normalized;
}

void PlayerSkills()
{
    if (player.isGrounded && Input.GetButtonDown("Jump"))
    {
        fallVelocity = JumpForce;
        movePlayer.y = fallVelocity;
        DoubleJump = true;
    }
    else if (player.isGrounded == false && Input.GetButtonDown("Jump") && DoubleJump == true)
    {
        fallVelocity = JumpForce *2;
        movePlayer.y = fallVelocity;
        DoubleJump = false;
    }
}
    
void setGravity()
{
    if (player.isGrounded)
    {
        fallVelocity = -gravity * Time.deltaTime;
        movePlayer.y = fallVelocity;
    }
    else
    {
        fallVelocity -= gravity * Time.deltaTime;
        movePlayer.y = fallVelocity;
    }
}

Thanks in advance!

Just so the answer to the question is not in the comments:

The original problem is that the assignment gameObject.transform.position = PointSpawn appeared to do nothing. As the line is written properly, the position of this gameObject , must have been getting overwritten elsewhere.

With the addition of OP's movement script, the position of the player was getting overwritten in the movement's Update function. As the other assignment was being done in Update , the call order was not guaranteed to work as intended. The fix is either to assure that the movement Update is run not the frame of the new position assignment or to move the conditional and the assignment to a function that always runs after Update regardless of script execution order, LateUpdate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM