Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Syntax error, ',' expected I can't find it in my script.

Discussion in 'Getting Started' started by ChuckOldBoy, Apr 23, 2024.

  1. ChuckOldBoy

    ChuckOldBoy

    Joined:
    Apr 23, 2024
    Posts:
    17
    I've got a script that keeps the main camera within a boundary.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraFollow : MonoBehaviour
    {
    PositionClamp cameraClamp;
    public float followSpeed;
    public Transform target;

    public float WORLD_MIN_X = -50.0f;
    public float WORLD_MIN_Y = -50.0f;
    public float WORLD_MAX_X = 50.0f;
    public float WORLD_MAX_Y = 50.0f;
    void Start()
    {
    cameraClamp = new PositionClamp(WORLD_MIN_X, WORLD_MIN_Y, WORLD_MAX_X, WORLD_MAX_Y, Camera Cam);
    }
    // Update is called once per frame
    void Update()
    {
    Vector3 newPos = new Vector3(target.position.x, target.position.y, -10);

    transform.position = Vector3.Slerp(transform.position, newPos, followSpeed * Time.deltaTime);

    cameraClamp.limitMovement(target, transform.position);
    }
    }

    This script references to another script, which handles the logic of it, a lot.

    using UnityEngine;
    using System.Collections;

    public class PositionClamp
    {
    // calculated position boundaries in world coordinates
    float minX;
    float minY;

    float maxX;
    float maxY;

    // constructor used to clamp a sprite
    public PositionClamp(float worldMinX, float worldMinY, float worldMaxX, float worldMaxY, Renderer r)
    {
    // calculate half sprite width and height from renderer
    float halfSpriteWidth = r.bounds.size.x / 2.0f;
    float halfSpriteHeight = r.bounds.size.y / 2.0f;

    // calculate and save boundaries
    minX = worldMinX + halfSpriteWidth;
    minY = worldMinY + halfSpriteHeight;

    maxX = worldMaxX - halfSpriteWidth;
    maxY = worldMaxY - halfSpriteHeight;
    }

    // constructor used to clamp a camera
    public PositionClamp(float worldMinX, float worldMinY, float worldMaxX, float worldMaxY, Camera cam)
    {
    // calculate half camera width and height, in world dimensions
    float halfCameraHeight = cam.orthographicSize;
    float halfCameraWidth = halfCameraHeight * cam.aspect;

    // calculate and save boundaries
    minX = worldMinX + halfCameraWidth;
    minY = worldMinY + halfCameraHeight;

    maxX = worldMaxX - halfCameraWidth;
    maxY = worldMaxY - halfCameraHeight;
    }

    // limit the input position to the defined world boundaries
    // and update the provided Transform with the results
    public void limitMovement(Vector3 targetPosition, Transform trans)
    {
    // Clamp the X and Y coordinates and make sure they
    // do not go beyond calculated boundaries
    float clampedX = Mathf.Clamp(targetPosition.x,minX,maxX);
    float clampedY = Mathf.Clamp(targetPosition.y,minY,maxY);

    // update transform position
    trans.position = new Vector3 (clampedX, clampedY, trans.position.z);
    }

    }

    The error is in the first script. I just can't find it please help.
     
  2. ChuckOldBoy

    ChuckOldBoy

    Joined:
    Apr 23, 2024
    Posts:
    17
    Here is a better view of each script please help me
     

    Attached Files:

  3. ChuckOldBoy

    ChuckOldBoy

    Joined:
    Apr 23, 2024
    Posts:
    17
    Note: I know there's a lot of problems with the way I presented this question. it's the first time I've ever asked a question on a forum.
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,185
    So.. it tells you the line and character position.. as part of the error.. start there, and tell us where there is
     
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,185
    PS Im gonna guess
    Code (CSharp):
    1. cameraClamp = new PositionClamp(WORLD_MIN_X, WORLD_MIN_Y, WORLD_MAX_X, WORLD_MAX_Y, Camera Cam);
    what the 'Camera Cam' for?
     
  6. ChuckOldBoy

    ChuckOldBoy

    Joined:
    Apr 23, 2024
    Posts:
    17
    Yeah I figured that out and it was the camera cam crap.

    Code (CSharp):
    1. cameraClamp = new PositionClamp(WORLD_MIN_X, WORLD_MIN_Y, WORLD_MAX_X, WORLD_MAX_Y, GetComponent<Camera>());
    this worked.

    thanks for responding.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,565
    Next time, format your code using this. It's not too late to edit your initial post, either.
    code-tag.png
     
    hasanerzi and bugfinders like this.