- Home /
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;
}
}
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?
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.
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.
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?
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.
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)
should i declare private variables for $$anonymous$$ and max?
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;
}
}
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 :(
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.
Your answer
Follow this Question
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