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.
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
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.
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.
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 ;
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.