[英]"error CS0103: The name 'targetAngle' does not exist in the current context" in unity 3d while was trying to make a 3rd person camera
So, I've been trying to make a third-person camera for the game I'm making in Unity 3D.所以,我一直在尝试为我在 Unity 3D 中制作的游戏制作第三人称相机。 I'm new to game dev(but I understand some things in this because I tried to make games earlier and tried to learn Java a bit) so I used Brackeys' guide on this , but every time I'm trying to compile the code, it just says that variable targetAngle where's stored math Atan function cannot be used in another variable moveDir for some reason.
我是游戏开发新手(但我了解其中的一些内容,因为我尝试更早地制作游戏并尝试学习一点 Java)所以我使用了 Brackeys 的指南,但每次我尝试编译代码时,它只是说由于某种原因,存储数学 Atan 函数的变量targetAngle不能在另一个变量moveDir中使用。 this is the error "Assets\Scripts\PlayerMovement.cs(67,48): error CS0103: The name 'targetAngle' does not exist in the current context", and here's the code
这是错误“Assets\Scripts\PlayerMovement.cs(67,48): error CS0103: The name 'targetAngle' does not exist in the current context”,这里是代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float _doubleJumpMultiplier = 1.5f;
public float turnSmoothTime = 0.1f;
private float turnSmoothVelocity;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
private bool _canDoubleJump = false;
bool _playerMove = false;
private void Start() {
_playerMove = GetComponent<PlayerMovement>();
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0) {
_canDoubleJump = true;
velocity.y = -2f;
}
else {
if(Input.GetButtonDown("Jump") && _canDoubleJump) {
velocity.y = 3f * _doubleJumpMultiplier;
_canDoubleJump = false;
}
}
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f) {
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir * speed * Time.deltaTime);
if(Input.GetButtonDown("Vertical") && isGrounded && _canDoubleJump) {
_playerMove = true;
}
else
if(!Input.GetButtonDown("Vertical") && isGrounded && _canDoubleJump) {
_playerMove = false;
}
if(Input.GetButtonDown("Horizontal") && isGrounded && _canDoubleJump) {
_playerMove = true;
}
else
if(!Input.GetButtonDown("Horizontal") && isGrounded && _canDoubleJump) {
_playerMove = false;
}
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
You define targetAngle
in this if statement:您在此 if 语句中定义
targetAngle
:
if(direction.magnitude >= 0.1f) {
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
And attempt to use it outside of it in the following line.并尝试在下面的行中使用它。 But here
targetAngle
is out of scope.但是这里的
targetAngle
超出了范围。
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.