The question is answered, right answer was accepted
Help with turning this into only x
I need someone to turn this code into so it only moves on the x-axis and not the y-axis. { public float speed = 10f; private Vector3 targetPosition; private bool isMoving; public GameObject clickAnimation; // Use this for initialization void Start() {
}
// Update is called once per frame
void Update()
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z = transform.position.z;
if (isMoving == false)
{
isMoving = true;
Instantiate(clickAnimation, targetPosition, Quaternion.identity);
}
if (isMoving == true)
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
if (targetPosition == transform.position)
{
isMoving = false;
Destroy(GameObject.Find("Click Animation(Clone)"));
}
}
}
Answer by streeetwalker · May 04, 2020 at 09:16 PM
@boss2070301, you can't directly set the transform.position.x, so you have to store your move position in a Vector3 first. then you want to use the x position from that result together with the current y and z position of your transform.
There are lots of ways you can cut this and end up with the same pie, so here is one way in pseudo code
Vector3 newPos = your move pos function call
newPos = new Vector3( newPos.x, transform.pos.y, transform.pos.z )
transform.pos = newPos
That will keep the same y and z but x will change
Also this line of code will probably never be true no matter how you move your objects, so better to check the distance between your target and your transform against some threshold, again in pesudo code:
// won't be ever true unless you explicitly set their x, y, and z
// exactly the same non decimal floats
if (targetPosition == transform.position)
// do this instead
if( Vector3.Distance( targetPos , trans.pos ) <= 0.001f )
// will be true if they are within 1mm of each other
// change the threshold to the value you like
@streeetwalker can you put the correct version of the code and implement it into my code please I am new so I don't know how to do so.
Thanks
@streeetwalker please help me I didn't understand your explanation can you just tell me what line to put the first code in the second one I understand but first I didn't so please help me.
I did answer your question! You have to learn how to take pseudo code and and make it work. If you pasted my 3 lines of code into your editor, it would basically tell you what you need to fix. br>
so 1) take this your line of code:
transform.position = **Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, speed Time.deltaTime);*
and replace it with my 3 lines of code.
2) then where I stated "your move pos function call" - replace that with your $$anonymous$$oveTowards function all bold text above.
3) Then every place you see transform.pos in my code, make sure to spell position completely, because there is no "transform.pos" - there is 'transform.position'
We write that pseudo code because it is faster to explain things, and we assume you should be able to figure all that out!
I assigned everything but the game still does the same thing. @streeetwalker
if (is$$anonymous$$oving == true)
{
Vector3 newPos;
transform.position = newPos = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, speed * Time.deltaTime);
newPos = new Vector3(newPos.x, transform.position.y, transform.position.z);
transform.position = newPos;
}
if (Vector3.Distance(targetPosition, transform.position) <= 0.001f)
{
is$$anonymous$$oving = false;
}