Search Unity

Having trouble with a 'lerp' script?

Discussion in 'Scripting' started by Treasureman, Oct 30, 2014.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Hi
    I have a script that should toggle my camera from point A to point B when the input 'SwitchShoulders' is pressed.

    Here's the script:
    Code (CSharp):
    1. public class TogglePosition : MonoBehaviour
    2. {
    3.     public Vector3 leftPosition = new Vector3(-4177.642f, -3220.743f, 6346.121f);
    4.     public Vector3 rightPosition = new Vector3(5159.432f, -3220.743f, 5764.928f);
    5.     public float speed = 1000f;
    6.    
    7.     void Update()
    8.     {
    9.         if (Input.anyKeyDown)
    10.             movingToRightPosition = !movingToRightPosition; // Go towards other position.
    11.         Vector3 targetPosition = movingToRightPosition ? rightPosition : leftPosition;
    12.         transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
    13.     }
    14.    
    15.     bool movingToRightPosition;
    16. }
    But I keep getting the error:
    "Assets/SwitchShoulders.cs(1,31): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing a using directive or an assembly reference?"

    How do I fix this?
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    You're missing the UnityEngine namespace. Add the following line to the top of your code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. // declare your class here
    5.  
    Also, the bool movingToRightPosition should be declared before Update.