[英]Unity3d CharacterController Collider is not moving with the Player Mesh
i added a charactercontroller to my Player Mesh but for some reason the CharacterController is not moving with the Player Mesh.我在我的玩家网格中添加了一个角色控制器,但由于某种原因,角色控制器没有随玩家网格一起移动。
here's the code for character movements这是角色移动的代码
// Vars
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
private Vector3 moveDirection;
private Vector3 velocity;
[SerializeField] private bool isGrounded;
[SerializeField] private float groundCheckDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float gravity;
[SerializeField] private float jumpHeight;
// Ref
private CharacterController controller;
private Animator anim;
private void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
private void Update()
{
Move();
if (Input.GetKeyDown(KeyCode.Mouse0))
{
StartCoroutine(Attack());
}
}
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
float moveY = Input.GetAxis("Horizontal");
moveDirection = new Vector3(moveY, 0, moveZ);
moveDirection = transform.TransformDirection(moveDirection);
if (isGrounded)
{
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
Walk();
}
else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
Run();
}
else if (moveDirection == Vector3.zero)
{
Idle();
}
moveDirection *= moveSpeed;
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void Idle()
{
anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
}
private void Walk()
{
moveSpeed = walkSpeed;
anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);
}
private void Run()
{
moveSpeed = runSpeed;
anim.SetFloat("Speed", 1f, 0.1f, Time.deltaTime);
}
private void Jump()
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
private IEnumerator Attack()
{
anim.SetLayerWeight(anim.GetLayerIndex("Attack Layer"), 1);
anim.SetTrigger("Attack");
yield return new WaitForSeconds(0.9f);
anim.SetLayerWeight(anim.GetLayerIndex("Attack Layer"), 0);
}
[A picture for the CharacterController and Player Mesh Before I Start and Move-in [开始和搬入之前的 CharacterController 和 Player Mesh 的图片
[1]: https://i.stack.imgur.com/P3jzh.png [1] [1]: https : //i.stack.imgur.com/P3jzh.png [1]
[And That's after I enter game mode and move] game] [那是在我进入游戏模式并移动之后]游戏]
[2]: https://i.stack.imgur.com/Bg1g7.png [2] [2]: https : //i.stack.imgur.com/Bg1g7.png [2]
It looks like the player mesh is separated from the player GameObject (and therefore the character controller).看起来玩家网格与玩家游戏对象(以及角色控制器)是分开的。 Make sure that it's parented so that the transforms move together.
确保它是父级的,以便转换一起移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.