- Home /
Problem with FixedUpdate() and changing velocity (2d)
I'm trying to create a game which controls similarly to space shooters (moving in one direction along with camera). I wrote a script to prevent me from jumping out of the camera but there is a hiccup.
The pseudo-code:
Detects if player will jump over camera edge at current speed (works)
Translates player to edge and sets speed to the camera's speed (works)
Keeps player at edge (doesn't work).
For some reason, even though the speed of the camera and player are the same, the player will "slide" a bit out of bounds. It is especially bad at higher speed where the player can slip off altogether. The sliding is usually proportional to the speed of the camera itself but the actual sliding distance is random.
Code:
void FixedUpdate() {
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
movement = new Vector2 (speed.x * inputX + camSpeed, speed.y * inputY);
if(cam.transform.position.x - camWidth / 2 == transform.position.x)
{
Debug.Log ("lock");
if((speed.x * inputX) + camSpeed < 0)
{
movement.x = camSpeed;
}
}
else if(cam.transform.position.x - camWidth / 2 > transform.position.x + movement.x * Time.deltaTime)
{
transform.Translate(new Vector2 (cam.transform.position.x - camWidth / 2 - transform.position.x, 0));
movement.x = camSpeed;
}
GetComponent<Rigidbody2D>().velocity = movement;
}
The camera has a set velocity in another script
Edit: switching from velocity to translate makes the entire thing run perfectly, I can provide the modified code if anyone is interested
Can you not instantiate the player as a child of camera and only move camera to the right, with the player having independent y movement?
SetParent(camera);
Should be working if placed on the player script.
That would work but I want to be able to move along the x-axis as well. $$anonymous$$oreover, having both parent and child have separate velocities works differently than how some may imagine. I might as well recode the entire thing to use translate ins$$anonymous$$d.
set the Camera as a child to the Ship you can then move the camera freely to change its position
Or
Add a child obejct to the ship tagged with CameraHead and make a script called CameraFolow and write this
Transform cameraTarget;
//LateUpdate is called after update
//so we know the ship is in its new positon
Void LateUpdate()
{
transform.position = cameraTarget.Position;
}
drop this on you camera
and then in the field of the hierachy drag and drop the CameraHead object in now the camera will follow that point
this will allow you to offset from that posiiton for camera shake or allowing the ship to catch up or slow down.
Your answer
![](https://koobas.hobune.stream/wayback/20220611235940im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Velocity in rigidbody2d is not doing anything. 1 Answer
Physics in 2d arkanoid. Tangential and normal velocity 1 Answer
AddExplosionForce after FixedUpdate? 1 Answer
Movement Relative to Rotated Parent 1 Answer
2D Collider aren't exact? 6 Answers