Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Monoblanco · Jul 08, 2015 at 09:26 AM · javascriptmovementvector3variablesmovetowards

How to set a target position in Vector3.MoveTowards using a variable

so i want a target position for my camera to move to be -10 away from the current x position of an object. I got this and got stuck.

 function moveCamera () {
         var myVar : double;
         myVar= GameObject.Find("First Person Controller").transform.position.x;
         myVar-= 10;
         transform.position = Vector3.MoveTowards(transform.position, myVar, .05);
 }

The Consoles says that I cannot use myVar in the MoveTowards because it uses a Vector3 but then i cannot subtract the 10 from myVar if it is a vector3.

Thank you in advance.

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
1
Best Answer

Answer by voporak5 · Jul 08, 2015 at 03:52 PM

change myVar to Vector3

and also do this

    function moveCamera () {
              var myVar : Vector3;
              myVar= GameObject.Find("First Person Controller").transform.position;
              var myTarg = myVar.x -= 10;
              myVar = Vector3(myTarg, myVar.y, myVar.z);
              transform.position = Vector3.MoveTowards(transform.position, myVar, .05);
      }
 

I do mostly C# so I think that's it, I'm not 100% if the syntax is correct.

Comment
Add comment · Show 1 · 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 Monoblanco · Jul 08, 2015 at 10:24 PM 0
Share

line four the -= should just be a - but everything else worked

avatar image
0

Answer by kjakubison · Jul 08, 2015 at 04:30 PM

The method MoveTowards expects a Vector3 and not a double which is why you are having issues. Here is a link to the reference for MoveTowards in JS.

http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

I didn't test this but the following should fix your issue.

 function moveCamera () {
         var originalPos : Vector3;
         var targetPos : Vector3;
         
         originalPos = GameObject.Find("First Person Controller").transform.position;
         
         targetPos = Vector3(originalPos.x, originalPos.y - 10,originalPos.z);
         
         transform.position = Vector3.MoveTowards(transform.position, targetPos, .05);
     }

**As a side note. You may experience issues if this is not being used in an Update function as it will only move once instead of every frame.

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 Monoblanco · Jul 08, 2015 at 10:17 PM 0
Share

you don't have to worry the moveCamera function will have a for loop in it

avatar image kjakubison · Jul 09, 2015 at 07:09 PM 0
Share

sounds good!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Vector3 of moving object always returns (0,0,0) 1 Answer

Instantiated object isnt moving 1 Answer

Applying Constant Force on Multiple Axes Via Javascript? 1 Answer

How to make Vector3s communicate with Floats? 2 Answers

Error Vector3 does not contain MoveTowards 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