Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved]Moving Platform error

Discussion in 'Scripting' started by RPP, Dec 20, 2015.

  1. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Hello again, so today I'm working on moving some platforms. I have followed some tutorials and steps and have concluded with this code below. I receive four errors in which I am unsure how to fix. If anybody could help that would be awesome! (3) on line 32 (1) on line 34. I believe they have to do with conversion between variables and operands for the last one. I received these errors due to trying to modify an Outdated script from Unity 4.X

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovingPlatforms : MonoBehaviour
    5. {
    6.  
    7.     [SerializeField]
    8.     Transform movingPlatform;
    9.  
    10.     [SerializeField]
    11.     Transform startTransform;
    12.  
    13.     [SerializeField]
    14.     Transform endTransform;
    15.  
    16.     [SerializeField]
    17.     Transform platformSpeed;
    18.  
    19.     Vector3 direction;
    20.  
    21.     Transform destination;
    22.  
    23.     void Start()
    24.     {
    25.         SetDestination(startTransform); //move in direction set to motion
    26.     }
    27.  
    28.  
    29.     // Update is called once per frame
    30.     void FixedUpdate ()
    31.     {
    32.         movingPlatform.GetComponent<Rigidbody2D>().MovePosition(movingPlatform.position + direction * platformSpeed * Time.fixedDeltaTime);
    33.  
    34.         if(Vector3.Distance(movingPlatform.position, destination.position) < platformSpeed * Time.fixedDeltaTime) //compares distance to turn around platform
    35.         {
    36.             SetDestination(destination == startTransform ? endTransform : startTransform); // ? = if true or false then go that direction
    37.         }
    38.     }
    39.  
    40.     void OnDrawGizmos()
    41.     {
    42.         Gizmos.color = Color.green;
    43.         Gizmos.DrawWireCube(startTransform.position, movingPlatform.localScale); //creates starting wireCube in Green
    44.  
    45.         Gizmos.color = Color.red;
    46.         Gizmos.DrawWireCube(endTransform.position, movingPlatform.localScale);    //creates ending wireCube in Red
    47.     }
    48.  
    49.     void SetDestination(Transform dest)
    50.     {
    51.         destination = dest;
    52.         direction = (destination.position - movingPlatform.position).normalized;
    53.     }
    54. }
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What are the error messages? Copy them from the console & paste them here
     
  3. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    ERRORS BELOW:

    1) Assets/Scripts/Level2_Scripts/MovingPlatforms.cs(32,99): error CS0019: Operator `*' cannot be applied to operands of type `UnityEngine.Vector3' and `UnityEngine.Transform'

    2) Assets/Scripts/Level2_Scripts/MovingPlatforms.cs(32,60): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody2D.MovePosition(UnityEngine.Vector2)' has some invalid arguments

    3) Assets/Scripts/Level2_Scripts/MovingPlatforms.cs(32,60): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector2'

    4) Assets/Scripts/Level2_Scripts/MovingPlatforms.cs(34,86): error CS0019: Operator `*' cannot be applied to operands of type `UnityEngine.Transform' and `float'
     
  4. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    platformSpeed is a Transform type. It looks like you want it to be a Vector2, or a float. I can't tell which. Or, perhaps you're using the local position of the platformSpeed transform to determine the motion of the platform, in which case you'd want to multiple platformSpeed.localPosition.
     
  5. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    For all those who need an answer, I found out that unity was using an old API to calculate the way i wrote my codes equations, once i updated the API everything worked