Home > Enterprise >  "error CS0103: The name 'targetAngle' does not exist in the current context" in
"error CS0103: The name 'targetAngle' does not exist in the current context" in

Time:07-18

So, I've been trying to make a third-person camera for the game I'm making in 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. 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

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);
    }

}

CodePudding user response:

You define targetAngle in this if statement:

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.

        Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
  • Related