transform.position doesn't move player

試著忘記壹切 提交于 2021-02-10 14:36:21

问题


I have a Player (made out of an cylinder, some FirstCam, SecondCam and ThirdCam, for switching views when pressing a certain key - doesn't matter), which is located on a cube (wide enough to take a walk on it). There's nothing else below the cube.

This is how it looks like:

This is the Player's structure:

This is the inspector for Player:

I have the following script ResetToInitial.cs, attached to my Player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResetToInitial : MonoBehaviour {
    Vector3 pos;
    void Start() {
        pos = transform.position;
    }

    void Update() {
        if (transform.position.y < -10) {
            transform.position = pos;
        }
    }
}

and also some movement script PlayerMovement.cs that allows him to move up, down, left, right, jump and sprint.

This is PlayerMovement.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class PlayerMovement : MonoBehaviour {
    public CharacterController controller;
    Vector3 velocity;
    bool isGrounded;

    public Transform groundCheck;
    public LayerMask groundMask;
    public TextMeshPro State;
    public TextMeshPro State2;
    public TextMeshPro State3;

    // Start is called before the first frame update
    void Start() {
    }

    // Update is called once per frame
    void Update() {
        float groundDistance = 0.4f;
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if (isGrounded && velocity.y < 0) {
            velocity.y = -2f; 
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        float g = -20f;
        float jumpHeight = 1f;
        float speed = 6f;
        
        if (Input.GetKey(KeyCode.LeftShift)) {
            speed = 8f;
            State.text = "running mode";
            State2.text = "running mode";
            State3.text = "running mode";
        } else {
            speed = 5f;
            State.text = "walking mode";
            State2.text = "walking mode";
            State3.text = "walking mode";
        }

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);
        if (Input.GetButton("Jump") && isGrounded) {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * g);  
        }
        velocity.y += g * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}

ResetToInitial.cs is intended to teleport the Player to the initial position (back on the cube, the position recorded when the scene loads), after he falls of the cube. The problem is transform.position won't move the player anywhere. It won't do anything. I tried to output something in the last if to see if it works, and the output was printed, but still no teleportation happened.

What could be the problem?

来源:https://stackoverflow.com/questions/66017493/transform-position-doesnt-move-player

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!