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. Dismiss Notice

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

Discussion in 'Scripting' started by Nubz, Jan 7, 2015.

  1. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    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
    Code (JavaScript):
    1. var headBobEnabled : boolean = true;
    2. var headBobDistance : Vector2;
    3. var headbobSpeed : float;
    4.  
    5. private var localPos : Vector3;
    6. private var transformRef : Transform;
    7.  
    8.  
    9.  
    10.  
    11.  
    12.     function Awake()
    13.     {
    14.         transformRef = transform;
    15.         localpos = transformRef.localPosition;
    16.     }
    17.  
    18.     function Update()
    19.     {
    20.         var stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
    21.         var positionXCal = localPos.x + headBobDistance.x * Mathf.Sin(Time.time * headbobSpeed + Mathf.PI / 2);
    22.         transformRef.localPosition.x = (localPos.x * (1 - stationaryPosition)) + (positionXCal * (stationaryPosition));
    23.      
    24.         var positionYCal = localPos.y + headBobDistance.y * Mathf.Sin(Time.time * 2 * headbobSpeed);
    25.         transformRef.localPosition.y = (localPos.y * (1 - stationaryPosition)) + (positionYCal * (stationaryPosition));
    26.     }
    For the most part I can get it done but its the Update function that is giving me troubles.
     
  2. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
  3. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Then read it again, and look at your script, lines 22 and 25
     
    Nubz likes this.
  5. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HeadBob : MonoBehaviour
    5. {
    6.  
    7.     public bool bobEnabled = true;
    8.  
    9.     public float bobSpeed;
    10.     public Vector2 bobDistance;
    11.  
    12.  
    13.     private Vector3 localPos;
    14.     private Transform transRef;
    15.  
    16.  
    17.     void Awake()
    18.     {
    19.         transRef = transform;
    20.         localPos = transRef.localPosition;
    21.     }
    22.     void Update()
    23.     {
    24.         float stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
    25.        
    26.         float positionXCal = localPos.x + bobDistance.x * Mathf.Sin(Time.time * bobSpeed + Mathf.PI / 2);
    27.        
    28.         transRef.localPosition.x = (localPos.x * (1 - stationaryPosition)) + (positionXCal * (stationaryPosition));
    29.        
    30.        
    31.         float positionYCal = localPos.y + bobDistance.y * Mathf.Sin(Time.time * 2 * bobSpeed);
    32.        
    33.         transRef.localPosition.y = (localPos.y * (1 - stationaryPosition)) + (positionYCal * (stationaryPosition));
    34.     }
    35. }
    36.  

    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.
     
    Last edited: Jan 7, 2015
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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

    Code (CSharp):
    1. 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

    Code (CSharp):
    1. float stationaryPosition = //...
    2. float positionXCal = //...
    3. float newX = //...
    4. float newY = //...
    5.  
    6. transformRef.localPosition = new Vector3 (newX, newY, transformRef.localPosition.z);
     
    Nubz likes this.
  7. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    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
     
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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
     
    Nubz and Kiwasi like this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    Nubz likes this.
  10. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    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
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HeadBob : MonoBehaviour
    5. {
    6.  
    7.     public Vector2 bobDistance;
    8.  
    9.     public bool bobEnabled = true;
    10.  
    11.     public float bobSpeed;
    12.  
    13.     private Vector3 localPos;
    14.  
    15.     private Transform transRef;
    16.  
    17.  
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         transRef = transform;
    23.         localPos = transRef.localPosition;
    24.     }
    25.  
    26.  
    27.     void Update()
    28.     {
    29.         float stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
    30.         float positionXCal = localPos.x + bobDistance.x * Mathf.Sin(Time.time * bobSpeed + Mathf.PI / 2);
    31.  
    32.         transRef.localPosition = new Vector3(localPos.x * (1 - stationaryPosition)) + (positionXCal) * (stationaryPosition), transRef.localPosition.y, transRef.localPosition.z);
    33.     }
    34. }
    35.  
    and get this error
    Which yes i know means there is a , where it wants a ;
     
    Last edited: Jan 7, 2015
  11. Thoras

    Thoras

    Joined:
    Oct 20, 2014
    Posts:
    3
    There is an extra close bracket after stationaryPosition on line 32, isn't there?
     
    Nubz likes this.
  12. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Yeah I wrote that too fast and wasn't paying attention.
    DERP on me lol.
     
  13. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HeadBob : MonoBehaviour
    5. {
    6.  
    7.     public Vector2 bobDistance;
    8.  
    9.     public bool bobEnabled = true;
    10.  
    11.     public float bobSpeed;
    12.  
    13.     private Vector3 localPos;
    14.  
    15.     private Transform transRef;
    16.  
    17.  
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         transRef = transform;
    23.         localPos = transRef.localPosition;
    24.     }
    25.  
    26.  
    27.     void Update()
    28.     {
    29.         float stationaryPosition = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
    30.         float positionXCal = localPos.x + bobDistance.x * Mathf.Sin(Time.time * bobSpeed + Mathf.PI / 2);
    31.  
    32.         transRef.localPosition = new Vector3(localPos.x * (1 - stationaryPosition) + (positionXCal) * (stationaryPosition), transRef.localPosition.y, transRef.localPosition.z);
    33.  
    34.         float positionYCal = localPos.y + bobDistance.y * Mathf.Sin(Time.time * 2 * bobSpeed);
    35.         transRef.localPosition = new Vector3 (transRef.localPosition.x, localPos.y * (1 - stationaryPosition) + (positionYCal) * (stationaryPosition), transRef.localPosition.z);
    36.     }
    37. }
    38.  
     
    Last edited: Jan 7, 2015
    cmcpasserby, angrypenguin and Kiwasi like this.