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 wiggs1111 · Aug 10, 2015 at 04:20 AM · javascriptvector3vector

Comparing vector 3 locations

 if(Input.GetButtonDown("up")){
 
     //transform.position.currentPos.y = transform.position.currentPos.y+1;
     currentPos.y = currentPos.y+1;
     Debug.Log("currentPos"+ currentPos);
     Debug.Log("startPos" +startPos);
          if (currentPos.y != StartPos) { 
          Debug.Log("after currentPos.y!= startPos condional "+currentPos);
         transform.position.y = transform.position.y+1;
             currentPos= this.transform.position;
     }
         else if(currentPos == startPos){
             Debug.Log("yes");
             currentPos= this.transform.position;
         }
         
 }

I want to not allow my player to move up ones if the location they are moving to is the location of where startPos is.

Javascript

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 allenallenallen · Aug 10, 2015 at 04:25 AM 1
Share

You just need transform.position.y, no need for currentPos.y.

But in your case, your code should still work. Except that on line 7, you typed "StartPos" when it's "startPos"

And what exactly is startPos? Is it a 3D position? If so, shouldn't you be limiting it for all 3 axis?

avatar image wiggs1111 · Aug 11, 2015 at 01:29 AM 0
Share

$$anonymous$$y fault the StartPos was my fault not proofreading it. The game is developed in 3d but the z axis never changes. $$anonymous$$y else if statement never gets caught even when my character is at (0,-1,0) and debug log still spits out (0,0,0,) which is where startCube is. Start cube is where the level starts and I dont want my character to be able to talk through it.

1 Reply

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

Answer by Yword · Aug 11, 2015 at 04:58 AM

Is that you want to move the object in grid but it cannot move to its starting position? If so, I had made some modifications based on your script, you may test it out to see whether it can help you or not.

 #pragma strict
 
 private var startPos : Vector3;
 private var targetPos : Vector3;
 
 
 function Start ()
 {
     // Record the startPos
     startPos = transform.position;
 }
 
 
 function Update() 
 {
     // Set targetPos as the transform's current position
     targetPos = transform.position;
 
 
     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         targetPos.x -= 1;
     }
     else if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         targetPos.x += 1;
     }
     else if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         targetPos.y += 1;
     }
     else if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         targetPos.y -= 1;
     }
 
 
     // Set transform at targetPos if it is not startPos
     if (targetPos != startPos)
     {
         transform.position = targetPos;
     }
 }
Comment
Add comment · 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

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

How do I make an array of vectors? 1 Answer

Fall Damage Script Problem? 1 Answer

I keep getting: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(float, UnityEngine.Vector3, UnityEngine.Quaternion)' was found. 1 Answer

Vector based on the rotation of the main camera 1 Answer

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 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