按照教程制作摇摆和头部摆动武器。我已正确遵循它,但出现错误:预期类型或命名空间或文件结尾
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponSwayBob : MonoBehaviour
{
[Header("Sway")]
public float SwayIntensityX;
public float SwayIntensityY;
public float MaxSway;
public float MinSway;
[Header("Transform")]
public Transform Gun;
public BobOverride[] BobOverrides;
public float currentSpeed;
private float TimeX;
private float TimeY;
private float XPos;
private float YPos;
private Vector3 SmoothV;
private void Update()
{
foreach(BobOverride Bob in BobOverrides)
{
if(currentSpeed >= Bob.minSpeed && currently <= Bob.maxSpeed)
{
float bobMultiplier = (currentSpeed == 0) ? 1 : currentSpeed;
TimeX += Bob.SpeedX / 10 * Time.deltaTime * bobMultiplier;
TimeY += Bob.SpeedY / 10 * Time.deltaTime * bobMultiplier;
XPos = Bob.BobX.Evaluate(TimeX) * Bob.IntensityX;
YPos = Bob.BobY.Evaluate(TimeY) * Bob.IntensityY;
}
}
float XSway = -Input.GetAxis("Mouse X") * SwayIntensityX;
float YSway = -Input.GetAxis("Mouse Y") * SwayIntensityY;
XSway = Mathf.Clamp(XSway, MinSway, MaxSway);
YSway = Mathf.Clamp(YSway, MinSway, MaxSway);
XPos += XSway;
YPos += YSway;
}
private void FixedUpdate
{
Vector3 target = new Vector3(XPos, YPos, 0);
Vector3 desiredPos = Vector3.SmoothDamp(Gun.localPosition, target, ref SmoothV, 0.1f);
Gun.localPosition = desiredPos;
}
[System.Serializeable]
public struct BobOverride
{
public float minSpeed;
public float maxSpeed;
[Header("X")]
public float SpeedX;
public float IntensityX;
public AnimationCurve BobX;
[Header("Y")]
public float SpeedY;
public float IntensityY;
public AnimationCurve BobY;
}
}
这是我的玩家移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float groundDrag;
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
[Header("Script")]
public WeaponSwayBob SwayBobScript;
[Header("Orientation")]
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
MyInput();
if (grounded)
{
rb.drag = groundDrag;
else
rb.drag = 4;
}
SwayBobScript.currentSpeed = WeaponSway() ? currentSpeed : 0;
}
private void FixedUpdate()
{
MovePlayer();
}
private bool WeaponSway()
{
if(Input.GetAxisRaw("Horizontal") ! = 0 || Input.GetAxisRaw("Vertical") ! = 0)
{
if(grounded)
{
return true;
}
return false;
}
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
}
private void MovePlayer()
{
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
}
}
我预计代码运行正常,但是当我保存代码时,代码显示错误
括号关闭了函数,因为您忘记打开“else”括号。在else后面加一个{应该就可以了。
我将其添加为代码中的注释:
private void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
MyInput();
if (grounded)
{
rb.drag = groundDrag;
else
rb.drag = 4;
} /* <----------- This bracket closes the function because you forgot to open the "else" bracket. Add a { after the else and it should be fine. */
SwayBobScript.currentSpeed = WeaponSway() ? currentSpeed : 0;
}