- Home /
Not appropriate for UnityAnswers
[C#] PlayerRelativeControl conversion errors
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:
transform.positionWithLocalOffset assign attempt for 'Player' is not valid. Input positionWithLocalOffset is { NaN, NaN, NaN }.
UnityEngine.CharacterController:Move(Vector3)
PlayerRelativeControl:Update() (at Assets/Scripts/PlayerRelativeControl.cs:80)
transform.localPosition assign attempt for 'CameraOffset' is not valid. Input localPosition is { NaN, 1.000000, -0.069555 }.
UnityEngine.Transform:set_localPosition(Vector3)
PlayerRelativeControl:Update() (at Assets/Scripts/PlayerRelativeControl.cs:91)
Here is the code so far:
using UnityEngine;
using System.Collections;
[RequireComponent( typeof( CharacterController ) )]
public class PlayerRelativeControl : MonoBehaviour
{
public Joystick moveJoystick;
public Joystick rotateJoystick;
public Transform cameraPivot;
public float forwardSpeed = 6;
public float backwardSpeed = 3;
public float sidestepSpeed = 4;
public float jumpSpeed = 4;
public float inAirMultiplier = 0.25f;
public Vector2 rotationSpeed = new Vector2( 50, 25 );
private Transform thisTransform;
private CharacterController character;
private Vector3 cameraVelocity;
private Vector3 velocity;
void Start ()
{
thisTransform = (Transform)GetComponent( typeof(Transform) );
character = (CharacterController)GetComponent( typeof(CharacterController) );
}
void OnEndGame()
{
moveJoystick.Disable();
rotateJoystick.Disable();
this.enabled = false;
}
void Update ()
{
Vector3 movement = thisTransform.TransformDirection( new Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
movement.y = 0;
movement.Normalize();
Vector3 cameraTarget = Vector3.zero;
Vector2 absJoyPos = new Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
if( absJoyPos.y > absJoyPos.x )
{
if( moveJoystick.position.y > 0 )
{
movement *= forwardSpeed * absJoyPos.y;
}
else
{
movement *= backwardSpeed * absJoyPos.y;
cameraTarget.z = moveJoystick.position.y * 0.75f;
}
}
else
{
movement *= sidestepSpeed * absJoyPos.x;
cameraTarget.x = -moveJoystick.position.x * 0.5f;
}
if( character.isGrounded )
{
if( rotateJoystick.tapCount == 2 )
{
velocity = character.velocity;
velocity.y = jumpSpeed;
}
}
else
{
velocity.y += Physics.gravity.y * Time.deltaTime;
cameraTarget.z = -jumpSpeed * 0.25f;
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move( movement );
if( character.isGrounded )
{
velocity = Vector3.zero;
}
Vector3 pos = cameraPivot.localPosition;
//Debug.Log(pos.x)
pos.x = Mathf.SmoothDamp( pos.x, cameraTarget.x, ref cameraVelocity.x, 0.3f );
pos.z = Mathf.SmoothDamp( pos.z, cameraTarget.z, ref cameraVelocity.z, 0.5f );
cameraPivot.localPosition = pos;
if( character.isGrounded )
{
Vector3 camRotation = rotateJoystick.position;
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
thisTransform.Rotate( 0, camRotation.x, 0, Space.World );
cameraPivot.Rotate( camRotation.y, 0, 0 );
}
}
}
@GenOli, Your question (as it appears now) is not appropriate for UnityAnswers. It has the following problems:
No discernable question has been asked / not enough information. We cannot help you unless you tell us what isn't working, what you've already tried, and what you expect from the community.
Question is not of interest to other Unity users. Questions on UnityAnswers should be broadly applicable beyond your specific project.
Question is a 'fix my code for me' question. UnityAnswers (UA) is not Quality Assurance (QA) - it is up to you to write and bugfix your own code.
Please see the FAQ for more information on UnityAnswers standards for new questions: http://answers.unity3d.com/page/faq.html
For anyone needing help with this script.
I've moved discussion to forums:
http://forum.unity3d.com/threads/180587-C-PlayerRelativeControl-conversion-errors
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Convert this Javascript into C#? 1 Answer
Get game component type? 1 Answer
Calling C# Classes from JS 1 Answer