Lerp a variable in my script?

I’m trying to make a script that will lerp a variable titled “xOffset” n a script called “MouseOrbitOTS” from 0.75 to -0.75 when the input “Switch Shoulders” is tapped. Here’s the script I was working on…

using UnityEngine;
using System.Collections;

public class SwithShoulders : MonoBehaviour {
{
    private Vector3 newPosition;

    void Awake ()
    {
        newPosition = MouseOrbitOTS.xOffset
    }
   
    void Update ()
    {
        PositionCanging();
    }

    void PositionCanging();
    {
    Vector3 position1 = (0.75);
    Vector3 position2 = (-0.75)

        if (Input.GetButtonDown("Switch Shoulders")
            if(newPosition = position1)
             newPosition = position2
       
        if (Input.GetButtonDown("Switch Shoulders")
            if(newPosition = position2)
             newPosition = position1

        MouseOrbitOTS.xOffset = Vector3.Lerp(MouseOrbitOTS.xOffset, newPosition, Time.deltaTime);
    }
}

The problem is, I’m very new to CSharp, and I got these errors which I don’t know how to fix…

Assets/SwithShoulders.cs(5,1): error CS1519: Unexpected symbol {' in class, struct, or interface member declaration Assets/SwithShoulders.cs(11,9): error CS1525: Unexpected symbol }‘, expecting ;' Assets/SwithShoulders.cs(19,9): error CS1519: Unexpected symbol {’ in class, struct, or interface member declaration
Assets/SwithShoulders.cs(23,18): error CS1519: Unexpected symbol if' in class, struct, or interface member declaration Assets/SwithShoulders.cs(23,40): error CS1519: Unexpected symbol (’ in class, struct, or interface member declaration
Assets/SwithShoulders.cs(24,36): error CS1519: Unexpected symbol =' in class, struct, or interface member declaration Assets/SwithShoulders.cs(24,47): error CS1519: Unexpected symbol )’ in class, struct, or interface member declaration
Assets/SwithShoulders.cs(25,38): error CS1519: Unexpected symbol =' in class, struct, or interface member declaration Assets/SwithShoulders.cs(27,18): error CS1519: Unexpected symbol if’ in class, struct, or interface member declaration
Assets/SwithShoulders.cs(27,40): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Assets/SwithShoulders.cs(28,40): error CS1519: Unexpected symbol =’ in class, struct, or interface member declaration
Assets/SwithShoulders.cs(28,51): error CS1519: Unexpected symbol )' in class, struct, or interface member declaration Assets/SwithShoulders.cs(29,38): error CS1519: Unexpected symbol =’ in class, struct, or interface member declaration
Assets/SwithShoulders.cs(33,1): error CS8025: Parsing error

If it helps, this is the video I tried to use to learn how to do this…

Thanks!

Errors are on the form:

Path_To_Script ( line number, column number ) error ERRORCODE: Error_Explanation

So an error like this:
Assets/SwithShoulders.cs(5,1): error CS1519: Unexpected symbol `{’ in class, struct, or interface member declaration

Can be broken up into:

Assets/SwithShoulders.cs → The error is in the script “SwithShoulders.cs” in the folder “Assets”
(5, 1) → The error is on the fifth line, in the first column - ie. it’s the first letter on the line
error CS1519 → An error code. You can’t really read much out of this

And then the important part:
Unexpected symbol `{’ in class, struct, or interface member declaration

There’s a ‘{’ symbol here, and that doesn’t make any sense.

This is a perfectly fine error message. If you’re writing code in a proper IDE (MonoDevelop or Visual Studio are the most common ones), there should also be a big ol’ red line under the error. Although MonoDevelop is bad and might have randomly decided to drop a feature becuase it just does that.

If you look at your line 5, there’s a { there. There’s also a { on the end of line 4. Your class needs to be wrapped in a single set of braces, not two.

All of your errors are like that - either too many/few braces, or missing semicolons. If you double-click an error in the console, your script editing tool should open on the line with the error on it, which should help.

After you’ve fixed that, your code will still not work. This is because you’re using lerp wrong. That’s okay - most beginners does that. There’s a great article here explaining how to use lerp correctly, so I won’t reapeat it.

1 Like

That extra curly opening bracket on your class definition might be causing a few issues :wink:

I revised it and found a lot of missing / extra things in the script. Here’s my revised version…

using UnityEngine;
using System.Collections;

public class SwithShoulders : MonoBehaviour
{
    private Vector3 newPosition;

    void Awake ()
    {
        newPosition = MouseOrbitOTS.xOffset;
    }
   
    void Update ()
    {
        PositionCanging();
    }

    void PositionCanging()
    {
    Vector3 position1 = (0.75);
    Vector3 position2 = (-0.75);

        if (Input.GetButtonDown("Switch Shoulders")
            if(newPosition = position1)
            newPosition = position2;
       
        if (Input.GetButtonDown("Switch Shoulders")
            if(newPosition = position2)
            newPosition = position1;

        MouseOrbitOTS.xOffset = Vector3.Lerp(MouseOrbitOTS.xOffset, newPosition, Time.deltaTime);
    }
}

I still have 2 errors though, but I’m not sure how to fix it…

Assets/SwithShoulders.cs(24,22): error CS1525: Unexpected symbol if' Assets/SwithShoulders.cs(28,22): error CS1525: Unexpected symbol if’

I can’t just get rid of the ifs, can I?

Oh, by the way, I know how the compiler errors work. When I said I was new to CSharp I meant I’ve been using JavaScript. Sorry I didn’t specify. I’m just telling you that you don’t have to explain it so much