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 lh7746 · Feb 16, 2015 at 01:44 PM · 2dmovementtransformtransform.position

How can i fix a position of a gameobject after it meets its condition

when I press a button the object moves down and when I releases the button(Eventrigger Pointer UP) my gameobject moves up . but when it reached the condition:

 if (destPos.y > origPos.y) 
   {
          vInput = 0f;
   }

After this function is called I press the button but it no longer works. how can i make it work everytime i presses the button and i want to limit that the y position of destPos will not be greater than y axis of origPos. Hope someone can help me with this. Thanks in advance!

 void Update()
 {    
     if (origPos.x != 0) {
         Debug.Log(destPos.x);
         destPos = origPos;        
     }
     destPos.x = origPos.x;
     origPos = new Vector2 (origin.transform.position.x, origin.transform.position.y);
     destPos = new Vector2 (destination.transform.position.x, destination.transform.position.y);

     if (destPos.y > origPos.y) 
         {
         vInput = 0f;
 }

}

 void FixedUpdate()
 {
     Move(vInput);
 }
 
 public void Move(float verticalInput )
 {
     Vector2 moveVel = myBody.velocity;
     moveVel.y = verticalInput * speed;
     myBody.velocity = moveVel;
 }


 
 
 public void StartMoving(float verticalinput)
 {
     vInput = verticalinput;    
 }

}

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 Ben-Stoneman ♦♦ · Feb 16, 2015 at 03:09 PM 0
Share

So to help me and other potential helpers understand this better:

You have an object that goes up and down the screen and you want to stop the object from moving outside the bounds of the screen? Can you confirm this is correct?

avatar image lh7746 · Feb 16, 2015 at 03:41 PM 0
Share

Exactly mister Ben Stoneman... u got the point .. im actually making this as a 2d fishing game in the future, so i want the destPos object to move vertically only when the button is pressed. thank you for having wide understanding on the problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mmmpies · Feb 16, 2015 at 03:16 PM

What if you try this?

 if (destPos.y > origPos.y)
 {
     destPos = new Vector2 (destPos.x, origPos.y);
 }

Typed on my phone, so sorry for typos.

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 Mmmpies · Feb 16, 2015 at 04:01 PM 0
Share

Not sure I understand @lh7746 you want hold the y position but the movement in that image looks like its on x not y. Have I misunderstood what you're askin?

avatar image lh7746 · Feb 16, 2015 at 04:47 PM 0
Share

alt text

ohw, Im reallysorry to confuse you $$anonymous$$mmpies..its my fault.i just uploaded the wrong one.. below is the picture that indicates

when i actually presses the button,(the middle redbutton) it moves vertically and goes back as i releases. i think the problem is.

if (destPos.y > origPos.y) { vInput = 0f; }

when the destPos object is in vInput = 0f; and i keep on pressing the button. the object doesnt move vertically anymore, i want that everytime i press the button it moves vertically. and i can press over and over.. it seems that it only work ones?? i dont know how to fix this.

the picture above is the the movement of the player,(left and right) and i just want the destPos follows the same x - axis of the player.

untitled.png (4.4 kB)
avatar image
0

Answer by melkorinos · Feb 16, 2015 at 05:04 PM

You should use Clamp to restrict the position of any object you want.

 destYPosCorrect = Mathf.Clamp(destPos.y, minValue, maxValue)

Then you can use it as a variable of a Vector3, also remember to do it in update if your Y limits are changing within the game. Otherwise just preset min to a public variable , you can check what value to put by using the inspector (though hardcoding is bad practice)

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 lh7746 · Feb 16, 2015 at 05:12 PM 0
Share

should i declare private variables for $$anonymous$$ and max?

avatar image lh7746 · Feb 16, 2015 at 05:28 PM 0
Share

i get an error with this can you help me to make this out?

using UnityEngine; using System.Collections;

public class FishInstantiate : $$anonymous$$onoBehaviour {

 public float speed = 10, jumpVelocity = 10;
 public Layer$$anonymous$$ask player$$anonymous$$ask;
 public bool can$$anonymous$$oveInAir = true;
 Rigidbody2D myBody;
 float vInput = 0;
 private GameObject origin;
 private GameObject destination;
 private Vector2 origPos;
 private Vector2 destPos;
 private Vector2 vector;
 private Vector3 destPosCorrect;
 public Vector3 $$anonymous$$;
 public Vector3 max;


 void Start()
 {
     origin = GameObject.Find("Player");
     destination = GameObject.Find("Destination");
     myBody = this.rigidbody2D;



 }



 void Update()
 {    
     destPosCorrect = $$anonymous$$athf.Clamp (destPos, $$anonymous$$, max);


 

     origPos = new Vector2 (origin.transform.position.x, origin.transform.position.y);
     destPos = new Vector2 (destination.transform.position.x, destination.transform.position.y);



}

 void FixedUpdate()
 {
     $$anonymous$$ove(vInput);
 }
 
 public void $$anonymous$$ove(float verticalInput )
 {
     Vector2 moveVel = myBody.velocity;
     moveVel.y = verticalInput * speed;
     myBody.velocity = moveVel;
 }
 

 
 
 public void Start$$anonymous$$oving(float verticalinput)
 {
     vInput = verticalinput;    
 }

}

avatar image lh7746 · Feb 16, 2015 at 05:30 PM 0
Share

The error is : error CS1502: The best overloaded method match for UnityEngine.$$anonymous$$athf.Clamp(float, float, float)' has some invalid arguments error CS1503: Argument #1' cannot convert UnityEngine.Vector2' expression to type float'

sorry but im not really good at scripting :(

avatar image melkorinos · Feb 16, 2015 at 05:48 PM 1
Share

Yeah sorry try adding destPos.y since you want to clamp that value. Also the max value should not matter, hence set it high and the $$anonymous$$ value should be the y position of your sea level - 0.1f , so you are sure that the collission will be detected. Hope it is clear enough, i edited my answer as well.

avatar image lh7746 · Feb 21, 2015 at 03:59 PM 0
Share

TY. @melkorinos it WOR$$anonymous$$S!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem when rotating sprite and moving it foward 1 Answer

Sprite moving too far/fast 1 Answer

Moving an object on different axis? 1 Answer

Sprite is not shown moving 1 Answer

Need help in managing sprite transform. 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