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
0
Question by doomprodigy · Feb 16, 2011 at 12:06 PM · conversioncs1612unify

Script Conversion once more.

Hopefully this is the last time that I ask about getting help with a script conversion for a week or two :D.

I am converting the Aim Down Sights Script on Unify into C#. (Well Parts of it)

Here is the Java:

var gun : Transform; static var nextPos = 0.1; static var nextField = 60.0; static var nextPos2 = -1.55; var dampVelocity = 0.4; var dampVelocity2 = 0.4; var dampVelocity3 = 0.4;

function Update () { var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3); var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3); var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);

gun.transform.localPosition.x = newPos; gun.transform.localPosition.y = newPos2; Camera.main.fieldOfView = newField;

if (Input.GetButton("Fire2")) { //adjust viewpoint and gun position nextField = 55.0; nextPos = -0.13; nextPos2 = -1.45;

} else { //adjust viewpoint and gun position nextField = 60.0; nextPos = 0.1; nextPos2 = -1.55;

} }

and this is my c# thus far:

using UnityEngine; using System.Collections;

public class ADS : MonoBehaviour { public Transform gun; private float nextPos = 0.1f; private float nextField = 60.0f; private float nextPos2 = -1.55f; private float dampVelocity = 0.4f; private float dampVelocity2 = 0.4f; private float dampVelocity3 = 0.4f;

void Update (){ float newPos= Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, ref dampVelocity, .3f); float newField= Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, ref dampVelocity2, .3f); float newPos2= Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, ref dampVelocity3, .3f);

gun.transform.localPosition.x = newPos; gun.transform.localPosition.y = newPos2; Camera.main.fieldOfView = newField;

if (Input.GetButton("Fire2")) { //adjust viewpoint and gun position nextField = 55.0f; nextPos = -0.13f; nextPos2 = -1.45f;

} else { //adjust viewpoint and gun position nextField = 60.0f; nextPos = 0.1f; nextPos2 = -1.55f;

} } }

I am getting the error "ADS.cs(18,18): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable"

Unsure on what to do.

Cheers, Aaron Lewis

Comment
Add comment · Show 2
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 Jessy · Feb 16, 2011 at 12:45 PM 0
Share

JavaScript is not Java.

avatar image doomprodigy · Feb 16, 2011 at 06:40 PM 0
Share

I know, I just have a habbit of writing Java and not Javascript/Unityscript

1 Reply

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

Answer by Jessy · Feb 16, 2011 at 12:44 PM

The problem you are asking about is that in C#, you have to assign a complete Vector3 to transform.localPosition. You also have some weirdness going on with repetitive variable names and code, and you're assigning values outside of functions, which is not recommended. If you really are going to hard-code these (which I doubt, so get rid of [HideInInspector] and use the Inspector), then at least serialize the values so they have more of a chance of working.

[HideInInspector] [SerializeField] Vector3 nextPos = new Vector3(.1F, -1.55F); [HideInInspector] [SerializeField] float[] dampVelocity13 = {.4F, .4F};

for (int i = 0; i < 2; ++i) nextPos[i] = Mathf.SmoothDamp( gun.transform.localPosition[i], nextPos[i], ref dampVelocity13[i], .3F); gun.transform.localPosition = nextPos;

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 doomprodigy · Feb 17, 2011 at 04:19 AM 0
Share

Sorry for the Late Acceptance, worked like a charm :). Thank you for the explanation as well, it does help with understanding it

avatar image Jessy · Feb 17, 2011 at 06:54 AM 0
Share

Good! That's what this site is all about, I'd like to think! :-)

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

No one has followed this question yet.

Related Questions

error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable 0 Answers

PolyLine Script form Unify Wiki help... 1 Answer

Another Java to C# Conversion Question!! 1 Answer

Is there a Ragdoll Wizard source script ? 0 Answers

Why Frame per second script always show me 60FPS ? 3 Answers


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