Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by Twinny · Sep 06, 2013 at 09:23 PM · js-c#js - c#

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;
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Twinny · Sep 07, 2013 at 04:02 AM 0
Share

this works. thanks! i assumed i was completely off for some reason.

avatar image Penzin · Apr 05, 2014 at 07:10 AM 0
Share

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.

avatar image
0

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;
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunnybomb7670 · Sep 06, 2013 at 10:19 PM 0
Share

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);
avatar image Twinny · Sep 06, 2013 at 11:01 PM 0
Share

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"

avatar image Twinny · Sep 07, 2013 at 12:06 AM 0
Share

still stuck....could use some help

avatar image landon912 · Sep 07, 2013 at 01:30 AM 0
Share

You can't compare a Vector3 and float. You need to get the floats within Vector3 and then compare.

transform.position.x > screenBoundary;

avatar image Twinny · Sep 07, 2013 at 01:58 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges