简体   繁体   English

“错误 CS0103:当前上下文中不存在名称‘targetAngle’”在统一 3d 中尝试制作第三人称相机时

[英]"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.

相关问题 Unity错误CS0103:当前上下文中不存在名称`&#39; - Unity error CS0103: The name `' does not exist in the current context Unity3d错​​误CS0103:名称“ PeerState”在当前上下文中不存在 - Unity3d error CS0103: The name `PeerState' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误cs0103名称&#39;IEnumerator&#39;在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103:名称“ DebugDisplayString”在当前上下文中不存在 - error CS0103: The name 'DebugDisplayString' does not exist in the current context 错误CS0103:名称“ ressourcesText”在当前上下文中不存在 - error CS0103: The name “ressourcesText” does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM