- Home /
How would i convert this from JS into C#?
pretty simple code but i have no idea how to make a c# version. i tried making vector 3s for everything but that seemed way too inefficient.
var screenBoundary: float;
if(transform.position.x < -screenBoundary)
transform.position.x = -screenBoundary;
(transform.position.x > screenBoundary)
transform.position.x = screenBoundary;
Answer by Garrafote · Sep 07, 2013 at 03:37 AM
Simple
float screenBoundary;
// you need to store position on a local variable
var position = transform.position;
// then you can modify its fields
if(position.x < - screenBoundary)
position.x = -screenBoundary;
if(position.x > screenBoundary)
position.x = screenBoundary;
// and then deploy to the original variable
transform.position = position;
Fun fact:
your last code would actually work, but you was missing some .x and parentheses
if ((transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z)).x < -screenBoundary)
{
transform.position = new Vector3(-screenBoundary, transform.position.y, transform.position.z);
}
if ((transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z)).x > screenBoundary)
{
transform.position = new Vector3(screenBoundary, transform.position.y, transform.position.z);
}
anyway those attributions inside the if conditions aren't necessaries so you can simplify your code to look like this:
if (transform.position.x < -screenBoundary)
{
transform.position = new Vector3(-screenBoundary, transform.position.y, transform.position.z);
}
if (transform.position.x > screenBoundary)
{
transform.position = new Vector3(screenBoundary, transform.position.y, transform.position.z);
}
this works. thanks! i assumed i was completely off for some reason.
You could condense this a bit further by replacing the if statements with an inline set of conditional operators:
transform.position = (transform.position.x < -screenBoundary)? (new Vector3(-screenBoundary, transform.position.y, transform.position.z)) : ((transform.position.x > screenBoundary)? (new Vector3(screenBoundary, transform.position.y, transform.position.z)) : transform.position);
The statement won't modify transform.position if neither condition returns true.
Answer by flaviusxvii · Sep 06, 2013 at 10:03 PM
Easy one!
float screenBoundary;
if(transform.position.x < -screenBoundary)
transform.position.x = -screenBoundary;
(transform.position.x > screenBoundary)
transform.position.x = screenBoundary;
im pretty sure that wont work, you cant modify the x y or z of the position directly, you will need to store it in a variable like this :
transform.position = new Vector3(screenboundary,transform.position.y,transform.position.z);
OR
transform.position += new Vector3(screenBoundary,0,0);
heres my code, its not working for some reason
if(transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z) < -screenBoundary)
{
transform.position = new Vector3(-screenBoundary, transform.position.y, transform.position.z);
}
if(transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z) > screenBoundary)
{
transform.position = new Vector3(screenBoundary, transform.position.y, transform.position.z);
}
i get an error "Operator < cannot be applied to operands of type UnityEngine.Vector3 and float"
You can't compare a Vector3 and float. You need to get the floats within Vector3 and then compare.
transform.position.x > screenBoundary;
i gave up and decided to just use a clamp in order to keep the player (who in this case is a ship in a 2D vertical shooter) from moving off screen. probably not the most efficient method but w/e.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113718im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create a guitexture When clicked on 3d text? 0 Answers
js to C# converstion Problem 3 Answers
.js to C# conversion 4 Answers
Convert js to C# Serializer problem 1 Answer
Gameobject variable on C# 1 Answer