- Home /
C# Transform Issues
I'm trying to convert a Unityscript script into a C# script. I'm getting several errors with the transforms but I'm not sure why. I have the transforms stored in Player so I don't why it keeps telling me to store it in another variable when I already have. What is wrong with my script? I have commented out the the lines of code that are causing these errors.
using UnityEngine;
using System.Collections;
public class NewFullyDynamicParkour : MonoBehaviour {
public float ClimbMax;
public float VaultMax;
public float VaultMin;
public float distanceToGoFwd = 0.7f;
private bool CanVault;
private bool CanClimb;
private float DistancePlRayFwd;
private float DistancePlRayUp;
private Transform Player;
private CharacterController charCont;
private bool CanParkour;
public bool EnableRoll;
public float lastPos;
public string Roll;
public float HeightToRoll = 25;
private bool canRoll;
public float distanceDamage = 55;
public float maxFall;
private float max;
public GameObject CheckRay;
public float range;
private float maxH;
public bool Translate;
void Start()
{
Player = transform.gameObject.transform;
charCont = Player.GetComponent<CharacterController>();
}
void Update ()
{
if(CanParkour || charCont.isGrounded == false)
Translate = true;
else
Translate = false;
maxH = Mathf.Abs(max - Player.transform.position.y + charCont.height/2);
Debug.Log(maxH);
CharacterController charContr = Player.GetComponent<CharacterController>();
float radius = charCont.radius;
Vector3 p1 = transform.position + charCont.center;
Vector3 p2 = p1;
p2.y += VaultMax;
p1.y -= charCont.height/2f + 0.02f;
if (Physics.CapsuleCast(p1, p2, radius, transform.forward, range))
{
if(!Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), 0.7f))
{
if(!CanParkour)
{
if(Input.GetKeyDown(KeyCode.V))
{
//Player.transform.position.y = CheckRay.transform.position.y + charCont.height + 0.1f;
Player.transform.Translate(0, 0, distanceToGoFwd + 0.3f);
}
}
}
if(Physics.Raycast(CheckRay.transform.position, transform.TransformDirection (Vector3.forward), 0.7f))
{
CheckRay.transform.Translate(0, 8 * Time.deltaTime, 0);
max = Mathf.Max(max, CheckRay.transform.position.y);
}
if(maxH < ClimbMax)
{
Changing();
}
else
CanParkour = false;
if(maxH >= VaultMax)
{
CanVault = false;
CanClimb = true;
}
else
{
CanVault = true;
CanClimb = false;
}
}
else
{
CanParkour = false;
max = 0;
CheckRay.transform.position = new Vector3(CheckRay.transform.position.x, Player.transform.position.y, CheckRay.transform.position.z);
}
if(CanParkour)
{
if(CanVault == true)
{
if(Input.GetKeyDown(KeyCode.V))
{
Vault();
}
}
if(CanClimb == true)
{
if(Input.GetKeyDown(KeyCode.V))
{
Climb();
}
}
}
if(EnableRoll)
{
if(!charCont.isGrounded)
maxFall = Mathf.Max(Mathf.Abs(charCont.transform.position.y), maxFall);
else
lastPos = charCont.transform.position.y;
if(charCont.isGrounded) {
if(maxFall - lastPos > HeightToRoll){
if(maxFall - lastPos > distanceDamage)
{
Player.transform.SendMessageUpwards("GetBulletDamage", maxFall + distanceDamage*2, SendMessageOptions.DontRequireReceiver);
}
maxFall = 0;
}
}
}
}
IEnumerator Vault()
{
float upDist = CheckRay.transform.position.y + 0.2f;
float fwdDist = distanceToGoFwd;
Vector3 endPos = new Vector3(Player.transform.position.x, upDist, fwdDist);
Player.transform.position = Vector3.MoveTowards(Player.transform.position, endPos, 5 * Time.deltaTime);
yield return new WaitForSeconds(distanceToGoFwd);
Translate = false;
}
void Climb()
{
//Player.transform.position.y = CheckRay.transform.position.y + charCont.height + 0.1f;
Player.transform.Translate(0, 0, distanceToGoFwd + 0.3f);
Translate = false;
}
IEnumerator Changing()
{
yield return new WaitForSeconds(0.3f);
CanParkour = true;
}
}
Answer by fendorio · Apr 16, 2014 at 09:07 PM
Since you just dumped your script I cba to describe where to find this. Do a simple ctrl+F
else
{
CanParkour = false;
max = 0;
CheckRay.transform.position = Vector3(CheckRay.transform.position.x, Player.transform.position.y, CheckRay.transform.position.z);
}
This line:
CheckRay.transform.position = Vector3(CheckRay.transform.position.x,
Player.transform.position.y, CheckRay.transform.position.z);
You need to add the new keyword before Vector3, transform.position = a new Vector3.
So should be:
CheckRay.transform.position = new Vector3(CheckRay.transform.position.x,
Player.transform.position.y, CheckRay.transform.position.z);
With that correction I've got it to compile.
That fixed one error but what about the others? Here's a list of the other lines of code that were giving errors.
//Player.transform.position.y = CheckRay.transform.position.y + charCont.height + 0.1f;
//Vector3 endPos = Vector3(Player.transform.position.x, upDist, fwdDist);
//Player.transform.position.y = CheckRay.transform.position.y + charCont.height + 0.1f;
Ok come on lol use your head:
Vector3 endPos = Vector3(Player.transform.position.x, upDist, fwdDist);
Exactly the same error as before. Use the new keyword.
Is CheckRay assigned to in the editor? You make no assignment to that GameObject in the script so presumably it's null.
Yes CheckRay is assigned to in the editor. So that solves the vector3s. What about the player.transform.position.y?
//Player.transform.position.y = CheckRay.transform.position.y + charCont.height + 0.1f;
//Player.transform.position.y = CheckRay.transform.position.y + charCont.height + 0.1f;
Although not the most elegant solution this will fix your problem:
var number = CheckRay.transform.position.y + charCont.height + 0.1f;
Player.transform.position.y = number;
So assign the sum of to a variable, then assign that variable to the transform.position.y.
Vote and accept if it helped :P
Btw the two errors are the same, so apply that to both of them :)