- Home /
How do I freeze X-Axis movement?
In my Pong-Clone I am able to get my enemy paddle to follow the ball's movement on Y axis using a Vector3.MoveToward function but am unable to freeze it's movement toward the ball on the X axis. The freeze X position button in the rigidbody component doesn't work and this is probably due to the Vector3.MoveToward function being inside the void Update ().
Any suggestions?
using UnityEngine;
using System.Collections;
public class EnemyMove2Ball : MonoBehaviour {
public Transform target; // drag the target (Ball) here
float speed = 5.0f; // move speed
void Update()
{
transform.position = Vector3.MoveTowards
(rigidbody.position,
target.position,
speed*Time.deltaTime);
}
}
Answer by KhShani · Mar 14, 2014 at 01:33 PM
go to the rigidbody compnent of the gameobject and there u will find a property "constraints " just check the x axis. and your gameobject x axis movement would be freeze
Answer by Linus · Mar 14, 2014 at 10:48 AM
On line 14
//JS
transform.position.x = 0;
//C#
transform.position = new Vector3(0f, transform.position.y, transform.position.z);
Freeze position in the editor only applies to physics. But you don't use that, and probably should check isKinematic if you have not done that.
The example code is if the paddle is to be in a particular place on x axis. You could also store the x location before moving it, and change transform.position.x back to what it should be afterwards.
Won't work. You cannot modify just the x factor of a transform.position. Try it yourself.
Not in CSharp which he is using. You'll get an error message:
Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
and have to assign the whole position.
at Linus: You should switch to CSharp if you are working with Unity for years already :) Can't believe how to survive on JS for such a long time :) Ok, blame-mode-off :)
Updated answer. Thanks for pointing out, I could have sworn it would work the same in C#.
Answer by benni05 · Mar 14, 2014 at 10:49 AM
That's it. A non-kinematic Rigidbody should be moved by for example AddForce (and related) function. Then it will take your freeze-settings take into account. Also call this function preferably in the FixedUpdate() method. What you are doing is to directly manipulate the transform.position which is possible as well. In this case you could do the following in order to maintain the x-position.
Vector3 newPosition = Vector3.Lerp(transform.position, target.position, speed*Time.deltaTime);
transform.position = new Vector3(transform.position.x, newPosition.y, transform.position.z);
This way there would be no need for a Rigidboy and the z position is maintained as well.
He might need Rigidbody for collision and collider detection.
I think Benni's add force suggestion applies more to this game. I need to read up on Lerp as well. Currently I have rigid bodies on everything, with is kinematic turned off, and my collider methods are on the ball itself, which generates random forward motion from a range on both x and y.
I was able to get something that works well enough for me to finish up. But I got a question as well.
If I were to make another Pong project, and apply Benni's provided script, I wouldn't need rigidbodies for any objects right? What's the easiest way to make the ball move without collision detection?
There is no need for Rigidbodies if you want exact(!) control over the movement and behaviour of your Transforms. Using the physics engine means you can relatively easy let your Transforms perform realistically looking movements, acceleration, bounces etc. But it comes at a price, for example collision detection problems for fast moving objects (going through wall etc.) and a general performance overhead (though only when it really gets complicated/crowded). In a 2D-Pong-like game I would prefer to have precise control because you do not want the ball enter slightly into the bat (could happen with Physics), you would like to bounce it off pixel-perfect and crisp. The movement would be acchieved by those Lerp and Slerp methods (or the $$anonymous$$oveTowards you have used which is basically a Lerp with an additional parameter).
Answer by Bluewell · Mar 14, 2014 at 11:10 AM
Try this in your Update() function, so it only moves along y and z axis and having a x axis position value of '0' for target Vector3;
void Update()
{
transform.position = Vector3.MoveTowards
(transform.position,
new Vector3(0, target.position.y,target.position.z),
speed*Time.deltaTime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613140223im_/https://answers.unity.com/themes/thub/images/avi.jpg)