UnityScript to C# problem

Hi I have converted my UnityScript camera to C# BUT it is not correct as the distance values wont get read ? any Ideas please ,this is killing me hahah …

UnityScript

var target : Transform;

function FixedUpdate () {
transform.position = Vector3.Lerp (transform.position, target.position + Vector3.up * 2, Time.deltaTime * 160 * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position, target.up), Time.deltaTime * 2);
transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position)+5; // 10 was 2
if (transform.position.x <=10) transform.position.x =10;
if (transform.position.z <=10) transform.position.z =10;
}

c#

using UnityEngine;
using System.Collections;

public class Carcam : MonoBehaviour {
Transform target;

void FixedUpdate (){
transform.position = Vector3.Lerp (transform.position, target.position + Vector3.up * 2, Time.deltaTime * 160 * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position, target.up), Time.deltaTime * 2);
transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position)+5; // 10 was 2
if (transform.position.x <=10) transform.position.x =10;
if (transform.position.z <=10) transform.position.z =10;
}
}

Use code tags like this when posting code, it makes it easier to read.

The problem you’re having is due to the way the “position” property works. Like other properties it is, essentially, a pair of get/set functions that masquerade as a single variable for better readability. This means that when you are trying to set your position.x, what you’re actually doing as far as the compiler sees is (to use a made-up function):

transform.GetPosition().x = 10;

But Vector3’s are passed by value, so GetPosition() here would return a copy of the position. You’re setting the X position of that copy, but like changing something on a xerox, the original is unaffected.

What you have to do is get the position, change what you want to change, and then assign the whole Vector3 to position.

Vector3 pos = transform.position;
pos.y = Terrain.activeTerrain.SampleHeight(pos)+5;
if (pos.x <= 10f) pos.x = 10f;
if (pos.z <= 10f) pos.z = 10f;
transform.position = pos;

UnityScript had a unique “hack” in place specifically to make that sort of code work, which Unity was able to do because they invented the language, but C# doesn’t support that sort of hackery.

1 Like

C# can be a little less forgiving with data types. Try changing all your constant numbers to be floats like 2.0f instead of 2, 5.0f instead of 5, etc.

1 Like

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(5,5): Warning MSB3257: The primary reference “System.ComponentModel.Annotations” could not be resolved because it has a higher version “4.0.10.0” than exists in the current target framework. The version found in the current target framework is “4.0.0.0”. (MSB3257) (XNode)

C:\Terra22\New Unity Project (1)\CSC: Error CS1617: Invalid option ‘latest’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default (CS1617) (XNode)

Lol Now I am getting this :frowning: I when I try to save anything in mono editor I think I need a holiday haha

ahh I have dotnet 5 sooo I guess the Latest unity 2021 is needed since it is impossible to go backwards … I HATE Windows 10 ,back to 7 later the last good version lol …

Cheeky but could you save this as a c# script please as i cannot compile anything right now cos of dotnet 5 ^^ and i just want to see if this works before i completely format my c drive and start again lol…or even put the fixed errors in their correct place ?,brain is fried :frowning:

The last really solid version of Windows was Windows 2000 Server SP4, released in 2003. Data centers would literally run it for years without rebooting. Windows 7 was the best standard user desktop since. I ran Windows Server 2008 R2 for a few years. It was solid. Definitely better than 8 and maybe early 10, but the more recent releases of 10 are pretty good. Yeah, weird stuff happens sometimes, but you really don’t have much choice these days if you want support or to be able to run current versions of development tools.

1 Like

I drop the script into Unity after editing it with Notepad and end up with a different script 10 X as long …I am OFF to try Unreal Engine lol ,THAT seems stable and not a new release every other day …Cannot believe I wrote a game in 2013 using Unityscript that got 63.5 THOUSAND downloads !!! and NOW I cannot even compile a Script …BYE Unity you have turned into a bitch of Windows it seems :frowning: I am OUT !

.net framework is designed so multiple versions can be installed at the same time. If you’re told you already have a newer version, all you do is uninstall the newer version, install the one you want, then reinstall the newer version again. Takes just a few minutes.

But Unity doesn’t compile using .net framework anyway, so I don’t see what that has to do with anything. Unity uses its own custom version of Mono, which is entirely separate.

Since Unity actually has nothing to do with script editing, this makes no sense. You must have screwed up something in Notepad… and geez, don’t code in Notepad anyways. But good luck with Unreal. Don’t use Notepad there either by the way.

1 Like

It fails to Uninstall the latest dotnet lol …I am wiping Win10 from existence ,and going back to 7 and Then giving Unity ONE last chance .Although when UE5 gets released Unity is dead in the water for me anyway lol :slight_smile:

Windows 7 support has ended. It hasn’t received a security update in over a year, and that isn’t because new security problems haven’t been found and exploited. If you want to dump Win 10, I’d suggest Linux.

2 Likes

Just found out Unreal Ed is on LINUX …OMG game changer HOW did I not know this HAHAH GOODBYEWINDOWS ! FOREVER !!! EDIT …DotNet5 just uninstalled hahah who says Windows does not listen to EVERYTHING you type hahaha ^^ Too little Too Late Billy Boy :slight_smile: CYA !!!

Double edit

Details

Microsoft .NET Framework 4 is already a part of this operating system. You do not need to install the .NET Framework 4 redistributable. <A HREF=NOT HAPPENING">More information.

Same or higher version of .NET Framework 4 has already been installed on this computer.

HAHAHAH Screw this JOKE of an OS …

Where are you even hitting the .net framework problem you’re complaining about? Unity doesn’t even use it as far as I’m aware.

1 Like

Trust me it does lol ,Not my problem anymore … Should have binned Windows spyware bloated rubbish tears ago ,well NOW I see sense Lesson Learned

hoping THIS works lol

Important

Because the 4.x versions of the .NET Framework are in-place updates, you cannot install an earlier version of the .NET Framework 4.x on a system that already has a later version installed. For example, on a system with Windows 10 Fall Creators Update, you cannot install the .NET Framework 4.6.2, since the .NET Framework 4.7.1 is preinstalled with the operating system.

NOW it makes PERFECT SENSE lol …Win7 it is then …what a joke

Thanks for all the help guys and sorry for all the moaning lol …been one of those YEARS hahah

1 Like