- Home /
Make gameobject follow player on x-axis only
Hey,
I'm trying to make something like a 2D drop shadow, as seen in games like Turtles in Time and other beat-em-up games. I'm using a character controller, so I have code now that applies applies gravity, and instantiates a box collider prefab under the player's position at the time of pressing Space (basically at the player's feet).
I'm having two problems. One is that I can't get the shadow to move with the player while in the air, without childing it to the player, which doesn't work, since they'll never collide that way. I'm looking for a way to program the prefab so that once it's instantiated, it'll move with the player, along the x axis. My second problem is destroying the instantiated prefab, and I've coded it so that when the controller is grounded, it'll destroy the gameobject I instantiated, but doesn't work. Any ideas on how to fix?
Here's my code so far:
if (cInput.GetKey("Space"))
{
movementDirection.y *= jumpSpeed;
hasJumped = true;
gravityOn = true;
}
if(hasJumped)
{
if(gravityOn)
//This is jsut to apply gravity until it's grounded
for (int i = 0; i < 999; i = i + 1)
movementDirection.y -= gravity * Time.deltaTime;
//Here to make sure only one shadow is made
if (!limit)
{
Instantiate(jumpStop, _intPos, transform.rotation);
limit = true;
}
}
if(worldMover.isGrounded)
{
gravityOn = false;
DestroyObject(jumpStop);
}
worldMover.Move(movementDirection * Time.deltaTime);
I'm aware of the error in the jump code, but at the moment, getting the shadow movement and destruction correct is more important. Thanks in advance.
Here is a script that has one game object following another only on the 'X' axis. There is not enough info here for to say what is going wrong with your Destory.
var goPlayer : GameObject;
function Start () {
goPlayer = GameObject.Find("Player");
}
function Update () {
transform.position = new Vector3(goPlayer.transform.position.x, transform.position.y, transform.position.z);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613093228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D Platfomer Enemy follow player only on x-axis, c# 1 Answer
Jumping stops x movement 1 Answer
Unity 2018 2D Character Jump Glitch 1 Answer
HingeJoint2D.jointAngle flips between two values based on rotation 0 Answers
The player randomly freezes in place while other objects move ingame 1 Answer