<Solved Thank You For Making Me learn and not giving me the answer>Trouble with UnityScript To C#

I hate coming here begging for help writing scripts with all the info out there to look at but I can’t get this right and have tried more than a few times.

I have this script for a headbob for my fps project and it is this one part in the update that I can’t get converted over.
Works fine as it is but the rest of my scripts are C# and I would like to have them all in the same language.

The script in question

var headBobEnabled : boolean = true;
var headBobDistance : Vector2;
var headbobSpeed : float;

private var localPos : Vector3;
private var transformRef : Transform;





    function Awake()
    {
        transformRef = transform;
        localpos = transformRef.localPosition;
    }

    function Update()
    {
        var stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
        var positionXCal = localPos.x + headBobDistance.x * Mathf.Sin(Time.time * headbobSpeed + Mathf.PI / 2);
        transformRef.localPosition.x = (localPos.x * (1 - stationaryPosition)) + (positionXCal * (stationaryPosition));
     
        var positionYCal = localPos.y + headBobDistance.y * Mathf.Sin(Time.time * 2 * headbobSpeed);
        transformRef.localPosition.y = (localPos.y * (1 - stationaryPosition)) + (positionYCal * (stationaryPosition));
    }

For the most part I can get it done but its the Update function that is giving me troubles.

Answer here:

1 Like

That is really no help and was one of the things I already read.

Then read it again, and look at your script, lines 22 and 25

1 Like

I do appreciate you guys trying to help and not just giving me the answer so I will learn from this though.

This is the c# code I had so far but I get an error
And yes that lin IS unhelpful since when I write it the way that explains I get a new error added to my troubles besides the one I already have

using UnityEngine;
using System.Collections;

public class HeadBob : MonoBehaviour
{

    public bool bobEnabled = true;

    public float bobSpeed;
    public Vector2 bobDistance;


    private Vector3 localPos;
    private Transform transRef;


    void Awake()
    {
        transRef = transform;
        localPos = transRef.localPosition;
    }
    void Update()
    {
        float stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
       
        float positionXCal = localPos.x + bobDistance.x * Mathf.Sin(Time.time * bobSpeed + Mathf.PI / 2);
       
        transRef.localPosition.x = (localPos.x * (1 - stationaryPosition)) + (positionXCal * (stationaryPosition));
       
       
        float positionYCal = localPos.y + bobDistance.y * Mathf.Sin(Time.time * 2 * bobSpeed);
       
        transRef.localPosition.y = (localPos.y * (1 - stationaryPosition)) + (positionYCal * (stationaryPosition));
    }
}

And the error I get is
error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition’. Consider storing the value in a temporary variable.

You are correct in your assumption about floats. But that’s probably not your problem (this is one situation where the var keyword is valid in C#.

In C# you can’t modify Transform.position.x directly. You have to assign an entire new vector to Transform.position. So line 22 becomes

transformRef.localPosition = new Vector3(localPos.x * (1 - stationaryPosition)) + (positionXCal * (stationaryPosition), transformRef.localPosition.y, transformRef.localPosition.z);

For readability you might want to split this up into more lines

float stationaryPosition = //...
float positionXCal = //...
float newX = //...
float newY = //...

transformRef.localPosition = new Vector3 (newX, newY, transformRef.localPosition.z);
1 Like

I understand this (or maybe I don’t)
I get more errors added on when I write it that way for some reason.

Which is what I meant by that link not being of any help

If you don’t tell us what those errors are, we cant help.
The ‘correct’ solution has been posted here, so something must be going wrong

2 Likes

Can you post your complete translation to C#? I can scan it for errors.

Its common that fixing one error creates another one. Normally the compiler stops after it reaches an error, as there is no guarantee that the rest of the script can be parsed correctly. This is also why you only attempt to fix the first error each time.

1 Like

Oh boy I’ve thrown it out like 6 times so I’ll have to write it again I will edit this post in a few with what I have.
Not sure whether to laugh or flip the desk LOL.

I get this far

using UnityEngine;
using System.Collections;

public class HeadBob : MonoBehaviour
{

    public Vector2 bobDistance;

    public bool bobEnabled = true;

    public float bobSpeed;

    private Vector3 localPos;

    private Transform transRef;
  
  
  
  
    void Start()
    {
        transRef = transform;
        localPos = transRef.localPosition;
    }
  
  
    void Update()
    {
        float stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
        float positionXCal = localPos.x + bobDistance.x * Mathf.Sin(Time.time * bobSpeed + Mathf.PI / 2);

        transRef.localPosition = new Vector3(localPos.x * (1 - stationaryPosition)) + (positionXCal) * (stationaryPosition), transRef.localPosition.y, transRef.localPosition.z);
    }
}

and get this error

Which yes i know means there is a , where it wants a ;

There is an extra close bracket after stationaryPosition on line 32, isn’t there?

1 Like

Yeah I wrote that too fast and wasn’t paying attention.
DERP on me lol.

Here’s Proof that Nubz doesn’t give in easily Hahaha.
Thanks for the help everyone.
Just like my crouch script last night I wrote it right once and had one little parsing error and let it throw me off and think it was wrong since like Boredmormon said it wouldn’t get past that error to tell me the other 2 were fixed.

using UnityEngine;
using System.Collections;

public class HeadBob : MonoBehaviour
{

    public Vector2 bobDistance;

    public bool bobEnabled = true;

    public float bobSpeed;

    private Vector3 localPos;

    private Transform transRef;




    void Start()
    {
        transRef = transform;
        localPos = transRef.localPosition;
    }


    void Update()
    {
        float stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
        float positionXCal = localPos.x + bobDistance.x * Mathf.Sin(Time.time * bobSpeed + Mathf.PI / 2);

        transRef.localPosition = new Vector3(localPos.x * (1 - stationaryPosition) + (positionXCal) * (stationaryPosition), transRef.localPosition.y, transRef.localPosition.z);

        float positionYCal = localPos.y + bobDistance.y * Mathf.Sin(Time.time * 2 * bobSpeed);
        transRef.localPosition = new Vector3 (transRef.localPosition.x, localPos.y * (1 - stationaryPosition) + (positionYCal) * (stationaryPosition), transRef.localPosition.z);
    }
}
3 Likes