- Home /
How to make a camera Follow an Object moving in zigzag path?
I have a sphere which is moving upwards in zigzag path. I want my camera to follow the zigzag object. When I am setting the "adding offset value to player position option" method, it is not showing me the zigzag effect as I am setting the player.position.y = camera.position.y. please help...
Hi, I think you must also set the x position of the camera the same as the x player position.
Hi, I want to achieve this https://answers.unity.com/questions/1509391/camera-follow-gameobject-moving-in-angular-path.html
Answer by NoDumbQuestion · May 23, 2018 at 10:08 AM
5 minute solution. Not tested:
All you have to do is have an value that respresent zig zag value that increase as player move. When get too high, starting to decrease to lower, when too low, start to rise value. Add that value to Camera position
public Camera myMainCamera;
public float zigzagWidth = 5f; // must be positive
private float currentZigZagPos = 0;
public void MainPlayerMoveDistance(float deltaMove)
{
float offset = 0;
var pos = myMainCamera.transform.position;
currentZigZagPos += deltaMove;
if (currentZigZagPos > zigzagWidth)
{
offset = currentZigZagPos - zigzagWidth;
currentZigZagPos = zigzagWidth;
}
else if(currentZigZagPos < -zigzagWidth)
{
offset = currentZigZagPos + zigzagWidth;
currentZigZagPos = -zigzagWidth;
}
// X or Y axis whatever
pos.x += deltaMove - offset;
myMainCamera.transform.position = pos;
}
$$anonymous$$athf.Pingpong() is extremely useful in this case ;)
Well well. Unity doc is full of unknown. Good to know
Your answer
Follow this Question
Related Questions
Make a camera to Follow two players 2 Answers
Smooth camera on moving platform 1 Answer
Help make camera zoom smoother 3 Answers
Camera not following Player 1 Answer
Camera Follow (Not Exact) 1 Answer