Search Unity

[C#] PlayerRelativeControl conversion errors

Discussion in 'Scripting' started by GenOli, May 1, 2013.

  1. GenOli

    GenOli

    Joined:
    Apr 21, 2013
    Posts:
    139
    I've been trying to convert the code from the Penelope(?) tutorial to CSharp from JavaScript also known as the mobile touchpad controls.

    I get these two errors when trying to play:

    Code (csharp):
    1. transform.positionWithLocalOffset assign attempt for 'Player' is not valid. Input positionWithLocalOffset is { NaN, NaN, NaN }.
    2. UnityEngine.CharacterController:Move(Vector3)
    3. PlayerRelativeControl:Update() (at Assets/Scripts/PlayerRelativeControl.cs:80)
    4.  
    5. transform.localPosition assign attempt for 'CameraOffset' is not valid. Input localPosition is { NaN, 1.000000, -0.069555 }.
    6. UnityEngine.Transform:set_localPosition(Vector3)
    7. PlayerRelativeControl:Update() (at Assets/Scripts/PlayerRelativeControl.cs:91)
    Here is the code so far:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent( typeof( CharacterController ) )]
    5. public class PlayerRelativeControl : MonoBehaviour
    6. {
    7.         public Joystick moveJoystick;
    8.         public Joystick rotateJoystick;
    9.         public Transform cameraPivot;
    10.         public float forwardSpeed = 6f;
    11.         public float backwardSpeed = 3f;
    12.         public float sidestepSpeed = 4f;
    13.         public float jumpSpeed = 4f;
    14.         public float inAirMultiplier = 0.25f;
    15.         public Vector2 rotationSpeed = new Vector2( 50, 25 );
    16.        
    17.         private Transform thisTransform;
    18.         private CharacterController character;
    19.         private Vector3 cameraVelocity;
    20.         private Vector3 velocity;
    21.        
    22.         void Start ()
    23.         {
    24.                 thisTransform = (Transform)GetComponent( typeof(Transform) );
    25.                 character = (CharacterController)GetComponent( typeof(CharacterController) );
    26.         }
    27.        
    28.         void OnEndGame()
    29.         {
    30.                 moveJoystick.Disable();
    31.                 rotateJoystick.Disable();
    32.                 this.enabled = false;
    33.         }
    34.  
    35.         void Update ()
    36.         {
    37.                 Vector3 movement = thisTransform.TransformDirection( new Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
    38.                 movement.y = 0;
    39.                 movement.Normalize();
    40.                
    41.                 Vector3 cameraTarget = Vector3.zero;
    42.                 Vector2 absJoyPos = new Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
    43.                 if( absJoyPos.y > absJoyPos.x )
    44.                 {
    45.                         if( moveJoystick.position.y > 0 )
    46.                         {
    47.                                 movement *= forwardSpeed * absJoyPos.y;
    48.                         }
    49.                         else
    50.                         {
    51.                                 movement *= backwardSpeed * absJoyPos.y;
    52.                                 cameraTarget.z = moveJoystick.position.y * 0.75f;
    53.                         }
    54.                 }
    55.                 else
    56.                 {
    57.                         movement *= sidestepSpeed * absJoyPos.x;
    58.                         cameraTarget.x = -moveJoystick.position.x * 0.5f;
    59.                 }
    60.                
    61.                 if( character.isGrounded )
    62.                 {
    63.                         if( rotateJoystick.tapCount == 2 )
    64.                         {
    65.                                 velocity = character.velocity;
    66.                                 velocity.y = jumpSpeed;
    67.                         }
    68.                 }
    69.                 else
    70.                 {
    71.                         velocity.y += Physics.gravity.y * Time.deltaTime;
    72.                         cameraTarget.z = -jumpSpeed * 0.25f;
    73.                         movement.x *= inAirMultiplier;
    74.                         movement.z *= inAirMultiplier;
    75.                 }
    76.                
    77.                 movement += velocity;
    78.                 movement += Physics.gravity;
    79.                 movement *= Time.deltaTime;
    80.                 character.Move( movement );
    81.                
    82.                 if( character.isGrounded )
    83.                 {
    84.                     velocity = Vector3.zero;
    85.                 }
    86.                
    87.                 Vector3 pos = cameraPivot.localPosition;
    88.                 //Debug.Log(pos.x)
    89.                 pos.x = Mathf.SmoothDamp( pos.x, cameraTarget.x, ref cameraVelocity.x, 0.3f );
    90.                 pos.z = Mathf.SmoothDamp( pos.z, cameraTarget.z, ref cameraVelocity.z, 0.5f );
    91.                 cameraPivot.localPosition = pos;
    92.                
    93.                 if( character.isGrounded )
    94.                 {
    95.                         Vector3 camRotation = rotateJoystick.position;
    96.                         camRotation.x *= rotationSpeed.x;
    97.                         camRotation.y *= rotationSpeed.y;
    98.                         camRotation *= Time.deltaTime;
    99.                         thisTransform.Rotate( 0, camRotation.x, 0, Space.World );
    100.                         cameraPivot.Rotate( camRotation.y, 0, 0 );
    101.                 }
    102.                
    103.                
    104.         }
    105. }
    106.  
    Any help would be greatly appreciated, I have done a lot of searching and it seems not all of the Penelope scripts have been converted to C# properly and this is the last major one that hasn't to my knowledge.
     
    Last edited: May 1, 2013
  2. Julien-Lynge

    Julien-Lynge

    Joined:
    Nov 5, 2010
    Posts:
    142
    @GenOli,
    The forum is not necessarily a better place for your question. The problem isn't that UA doesn't accept 'write my code for me' questions and the forum does - the problem is that the community as a whole isn't here to do your work for you.

    We're totally willing to help you learn Unity so you can solve your own problems, and in fact there are a number of posts already that could help you. For instance, searching for 'unity convert js to c#' gives me: http://answers.unity3d.com/questions/21573/converting-code-from-javascript-to-c.html

    Also look at http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/
     
  3. GenOli

    GenOli

    Joined:
    Apr 21, 2013
    Posts:
    139
    Please accept my apologies, I have recently come from the ShiVa community (Stonetrip went into liquidation recently which was the final straw for me) where "help me write my code" questions are or at least seem to be entirely acceptable as when you are stuck on something, most of the time someone else has already posted a thread about it with a solution so they aren't just beneficial to the original poster but the community as well.

    So if someone else comes across this thread, instead of spending 2 days of scratching their head wondering why it doesn't work like I did, they can use this solution.

    Also regarding the original post, it wasn't anything to do with the code, turns out at the start of the log it said the joystick wasn't defined and something to do with the tag, so I added the tag "joystick" in the inspector and set the LeftJoystick and RightJoystick parts of the Player Relative Controls object tags to it and now it all works.
     
    Last edited: May 1, 2013
  4. Julien-Lynge

    Julien-Lynge

    Joined:
    Nov 5, 2010
    Posts:
    142
    Glad you figured it out!

    When you've been here for a little while, you'll realize that with 750,000 Unity users, we'd be flooded if we didn't try to force new users to write their own code. As long as you can keep your questions specific, we're happy to try and help you learn. However, if you post a bunch of code with a "why is this not working", you might provoke the ire of some in the community :)