Can't fix a parsing error?

I recently got a script that’s supposed to make the player align with the camera when the player moves. The problem is I have a parsing error that I’m having trouble fixing. Here’s my script

using UnityEngine;
using System.Collections;

public class CharacterAlign1 : MonoBehaviour {

    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
  
    }
}
//Return direction align with Camera
Vector3 targetDirection
{
    get
    {
        Vector3 cameraForward = mainCamera.TransformDirection(Vector3.forward);
        cameraForward.y = 0; //set to 0 because of camera rotation on the X axis
      
        //get the right-facing direction of the camera
        Vector3 cameraRight = mainCamera.TransformDirection(Vector3.right);
      
        //determine the direction the player will face based on input and the camera's right and forward directions
        return Input.GetAxis("Horizontal") * cameraRight + Input.GetAxis("Vertical") * cameraForward;
    }
}

//This method needs to run on Update
void RotateWhileMoving()
{
    if(speed > 0.5f)
    {
        //normalize the direction the player should face
        Vector3 lookDirection = targetDirection.normalized;
      
        //rotate the player to face the correct direction ONLY if there is any player input
        if (lookDirection != Vector3.zero)
        {
            newRotation = Quaternion.LookRotation(lookDirection);
          
            transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 4.5f * Time.deltaTime);
        }
    }
}

How do I fix it. It’s on line (18,1). I know I already asked about this n another thread, but this is a way less confusing one. Thanks!

The property targetDirection is declared outside of the CharacterAlign1 class. (And so is the RotateWhileMoving method)

  • targetDirection should really be a method instead of a property

  • mainCamera is never declared or initialized anywhere

  • When mainCamera is initialized properly you can skip the TransformDirection() method call and just use mainCamera.forward and mainCamera.right

  • speed is never declared or initialized

  • lookDirection is never declared or initialized

Did you just copy-paste some forum code into the bottom of one of your files?

1 Like

Parsing error is almost always a problem with matching {}. Move the } from line 15 to the end of the file. This will fix the parsing error. It will cause others, as indicated by @KelsoMRK