Hello!
Im working on a 2D platformer game and i found a script that made my character face the way it goes.
But it was in JS, and my knownledge in C# is limited. But i really want my script to be the same language so i don’t get problems.
So is there a way to rewrite this short script into C#?
#pragma strict
var X : float;
function Start () {
X = transform.localScale.x;
}
function Update () {
if(Input.GetKey(KeyCode.A)) {
transform.localScale.x = -X;
}
else if (Input.GetKey(KeyCode.D)) {
transform.localScale.x = X;
}
}
Already tested that, but i get a error.
Assets/Scripts/Test.cs(34,23): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localScale’. Consider storing the value in a temporary variable
Well it almost works, now on each click, the sprite faces where its movin, but he increases size on the X axis, and there is no stop.
Do you understand? or i will post a picture
Yeah but in the JS, it just rotates 90 degrees, and in the C# it rotates 90 degrees, after that it increases in X. Thats what you coded to but if you test both scripts u see the diffrence.
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour {
public float X;
void Start()
{
X = transform.localScale.x;
}
void Update()
{
if(Input.GetKey(KeyCode.A))
{
transform.localScale = new Vector3(-X, transform.localScale.y, transform.localScale.z);
}
else if (Input.GetKey(KeyCode.D))
{
transform.localScale = new Vector3(X, transform.localScale.y, transform.localScale.z);
}
}
}
Now there is a new problem i have tried to solve…
When i add the script, it works perfect, but when i save and restart unity, the player only change direction, he can’t walk.
It is like the script above wrote over my movement script or something. I should also add i got 2 diffrent script, this one for my rotation, the other one for movement , jumping. Im using same key to walk that i use in the rotation script above!