Search Unity

issue attaching scripts to gameobjects.

Discussion in 'Scripting' started by puddleglum, Jan 15, 2021.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    412
    i have two scripts that i found online. one is a physics object and the other is a player platform controller. when i try to add the physics object to a game object its fine. when i try to add the platform controller it gives me this error
    upload_2021-1-15_0-7-11.png

    Physics Object
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PhysicsObject : MonoBehaviour
    7. {
    8.  
    9.     public float minGroundNormalY = .65f;
    10.     public float gravityModifier = 1f;
    11.  
    12.     protected Vector2 targetVelocity;
    13.     protected bool grounded;
    14.     protected Vector2 groundNormal;
    15.     protected Rigidbody2D rb2d;
    16.     protected Vector2 velocity;
    17.     protected ContactFilter2D contactFilter;
    18.     protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
    19.     protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
    20.  
    21.  
    22.     protected const float minMoveDistance = 0.001f;
    23.     protected const float shellRadius = 0.01f;
    24.  
    25.     void OnEnable()
    26.     {
    27.  
    28.         rb2d = GetComponent<Rigidbody2D>();
    29.     }
    30.  
    31.     void Start()
    32.     {
    33.         contactFilter.useTriggers = false;
    34.         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
    35.         contactFilter.useLayerMask = true;
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         targetVelocity = Vector2.zero;
    41.         ComputeVelocity();
    42.     }
    43.  
    44.     protected virtual void ComputeVelocity()
    45.     {
    46.  
    47.     }
    48.  
    49.     void FixedUpdate()
    50.     {
    51.         velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
    52.         velocity.x = targetVelocity.x;
    53.  
    54.         grounded = false;
    55.  
    56.         Vector2 deltaPosition = velocity * Time.deltaTime;
    57.  
    58.         Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);
    59.  
    60.         Vector2 move = moveAlongGround * deltaPosition.x;
    61.  
    62.         Movement(move, false);
    63.  
    64.         move = Vector2.up * deltaPosition.y;
    65.  
    66.         Movement(move, true);
    67.     }
    68.  
    69.     void Movement(Vector2 move, bool yMovement)
    70.     {
    71.         float distance = move.magnitude;
    72.  
    73.         if (distance > minMoveDistance)
    74.         {
    75.             int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
    76.             hitBufferList.Clear();
    77.             for (int i = 0; i < count; i++)
    78.             {
    79.                 hitBufferList.Add(hitBuffer[i]);
    80.             }
    81.  
    82.             for (int i = 0; i < hitBufferList.Count; i++)
    83.             {
    84.                 Vector2 currentNormal = hitBufferList[i].normal;
    85.                 if (currentNormal.y > minGroundNormalY)
    86.                 {
    87.                     grounded = true;
    88.                     if (yMovement)
    89.                     {
    90.                         groundNormal = currentNormal;
    91.                         currentNormal.x = 0;
    92.                     }
    93.                 }
    94.  
    95.                 float projection = Vector2.Dot(velocity, currentNormal);
    96.                 if (projection < 0)
    97.                 {
    98.                     velocity = velocity - projection * currentNormal;
    99.                 }
    100.  
    101.                 float modifiedDistance = hitBufferList[i].distance - shellRadius;
    102.                 distance = modifiedDistance < distance ? modifiedDistance : distance;
    103.             }
    104.  
    105.  
    106.         }
    107.  
    108.         rb2d.position = rb2d.position + move.normalized * distance;
    109.     }
    110.  
    111. }
    112.  
    Platform Controller
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerPlatformerController : PhysicsObject
    7. {
    8.  
    9.     public float maxSpeed = 7;
    10.     public float jumpTakeOffSpeed = 7;
    11.  
    12.     private SpriteRenderer spriteRenderer;
    13.     private Animator animator;
    14.  
    15.     // Use this for initialization
    16.     void Awake()
    17.     {
    18.         spriteRenderer = GetComponent<SpriteRenderer>();
    19.         animator = GetComponent<Animator>();
    20.     }
    21.  
    22.     protected override void ComputeVelocity()
    23.     {
    24.         Vector2 move = Vector2.zero;
    25.  
    26.         move.x = Input.GetAxis("Horizontal");
    27.  
    28.         if (Input.GetButtonDown("Jump") && grounded)
    29.         {
    30.             velocity.y = jumpTakeOffSpeed;
    31.         }
    32.         else if (Input.GetButtonUp("Jump"))
    33.         {
    34.             if (velocity.y > 0)
    35.             {
    36.                 velocity.y = velocity.y * 0.5f;
    37.             }
    38.         }
    39.  
    40.         bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < 0.01f));
    41.         if (flipSprite)
    42.         {
    43.             spriteRenderer.flipX = !spriteRenderer.flipX;
    44.         }
    45.  
    46.         animator.SetBool("grounded", grounded);
    47.         animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
    48.  
    49.         targetVelocity = move * maxSpeed;
    50.     }
    51. }
    52.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Double check the filename. It has to match the code. PlayerPlatformerController vs PlayerPlatformController
     
  3. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    412
    that was it. im litteraly blind lol. i thought it was an issue with the class being "PhysicsObject"
     
  4. vbvbnbbbj

    vbvbnbbbj

    Joined:
    Apr 11, 2020
    Posts:
    2
    How to use your scripts to make UI buttons to move left , right and jump???
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Please don't hijack old topics for unrelated questions. Start your own topic... it's FREE!

    When you do, please read this carefully:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/