Hey guys!
I am doing some pretty straight forward movement using MoveTowards and I was wondering if I could fine tune it a bit.
So basically I have two game objects, moveMe which is the object doing the moving, and then I have targ which is the target object, where moveMe is moving towards.
This is how I am utilizing the MoveTowards method.
moveMe.transform.position = Vector3.MoveTowards(moveMe.transform.position, targ.transform.position, 2 * Time.deltaTime);
This worked great for when my project was new and not much was going on, but I have since discovered just by how it is set up, I don’t want moveMe to worry about it’s Y value. Just X and Z… as both game objects are on the ground. Is it possible to do the exact same thing, but omit the Y data? The main issue with it moving the Y, is on inclines like hills and such, the object will appear to be clipping down through them.
As always thanks for reading this thread! I am all ears!
Well, you could completely switch to rigidbody movement, and then completely get rid of the y value, because the issue you’re facing is most likely because you aren’t using physics to move the character, with rigidbody movent, it would look like this:
Rigidbody rb = GetComponent<Rigidbody>();
rb.velocity = (targ.transform.position - transform.position).normalized * speed;
It simply turns the distance between the target and the character into a vector, turns it into a normal vector, and multiplies it with speed
NOTE:
this solution might cause jittering, when the player is at the target, so i would implement a feature, that stops this movement, if the player is relatively close
OMG this one is far simpler than you could imagine…
just collect the y, then put it back when you finish the calculation
float y = moveMe.transform.position.y;
Vector3 newPos = moveMe.transform.position;
newPos.y = targ.transform.position.y;
newPos = Vector3.MoveTowards(newPos , targ.transform.position, 2 * Time.deltaTime);
newPos.y = y;
moveMe.transform.position = newPos;
Thanks for the replies guys!
I actually found a similar thread where someone was using the MoveTowards function and they only wanted the X position. I used that solution and modified it for only moving the X and Z!
@gorbit99 I did consider having RigidBody’s but with all the other stuff going on in the scene, and hearing that rigidbodies have a mind of their own, I decided to not use them. However… it would make hit detection much easier…
So for anyone looking for how to use MoveTowards with just X and Z, this is my solution and it works great! Keep in mind I am just feeding the object moving it’s current Y position to keep it the same!
//This will move the current object to the X,Y,Z Position of the target object
//moveMe.transform.position = Vector3.MoveTowards(moveMe.transform.position, targ.transform.position, 2 * Time.deltaTime);
//Same function, this however will NOT follow the Y position of the target, it will only move towards the X and Z of the target object!
moveMe.transform.position = Vector3.MoveTowards(moveMe.transform.position, new Vector3(targ.transform.position.x, moveMe.transform.position.y, targ.transform.position.z), 2 * Time.deltaTime);