你是翻译助手,只输出翻译结果,不加任何解释。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour
{
float inputH;
float inputV;
public float speed = 5f;
public Rigidbody rigidBody;
public float maxSlopeAngle;
private RaycastHit slopeHit;
public float groundDrag;
public float CarHeight;
public LayerMask WhatIsGround;
bool grounded;
public Transform orientation;
private Vector2 moveDirection;
private void Start()
{
CarHeight = transform.localScale.y; //设置汽车高度
}
private void Update()
{
//地面检查
grounded = Physics.Raycast(transform.position, Vector3.down, CarHeight * 0.5f + 0.2f, WhatIsGround);
//处理阻力
if (grounded)
{
rigidBody.linearDamping = groundDrag;
}
else
{
rigidBody.linearDamping = 0;
}
}
void FixedUpdate()
{
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
moveDirection = orientation.forward * inputV + orientation.right * inputH;
Vector3 movementInput = new Vector3(inputH * speed, rigidBody.linearVelocity.y, inputV * speed);
rigidBody.MovePosition(transform.position + movementInput * Time.deltaTime * speed);
if (OnSlope()) //当汽车处于斜面时
{
Debug.Log("正在调用");
rigidBody.AddForce(GetSlopeMoveDirection() * speed * 20f, ForceMode.Force);
}
}
private bool OnSlope()
{
if (grounded) //检查只能在汽车着地时发生
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, CarHeight * 0.5f + 0.3f))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal); //斜面角度
Debug.Log(angle);
return angle < maxSlopeAngle && angle != 0;
}
else
{
return false;
}
}
else
{
return false;
}
}
private Vector3 GetSlopeMoveDirection()
{
return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ground")
{
Debug.Log("碰撞");
}
else
{
Debug.Log("AAAAAA");
}
}
}
玩家控制器代码https://reddit.com/link/1x048d8/video/bu4qagf883uh1/player
我正在失去我的大脑。立方体和汽车都有刚体。它们都有非网格碰撞器。使用动态,但关闭了它,试图让它工作。你可以在我的代码和调试日志中看到立方体确实发生了碰撞,碰撞发生了,但汽车仍然穿过立方体!我不知道该怎么办。
评论 (0)