0
votes

I have developed a runner game in unity, i have completed the coding part, but when i applied the animation to my character object(which is rotation in z-axis) the camera also rotates with it. So, how can i stop this rotation of main camera which is child of my main runner object. Also i don't want any change in rotation of Main Camera

Main Camera Transform:-

  • The Initial Position is(3,13,-28).
  • The Initial Rotation is(20,0,0).
  • The Initial Scale is (0,0,0).

Some of the code i have tried in a separate script an attached it to main camera:-

  1. transform.parent = null;
  2. Camera.main.transform.rotation = Quaternion.identity;
  3. Camera.main.transform.eulerAngles = new Vector3(20,0f,0f);
  4. distanceTraveled1 = new Vector3(runnercode.distanceTraveled,0f,0f);
  5. Camera.main.transform.eulerAngles = new Vector3 (runnercode.distanceTraveled, 0f, 0f);(works,but rotation is always changes)

Any solution? i use C# scripting

1
Unity tag has nothing to do with Unity3d game engine on stack overflow. Please don't use it with this kind of question.Max Yankov

1 Answers

0
votes

Your code needs to be in LateUpdate if you want to be sure it corrects the rotation before the frame is rendered. But have you considered making a separate "FollowObject" script so the camera doesn't need to be a child object? You might find additional uses for it, too. Because you really don't want any of the other semantics of the parent/child relationship besides position.

And I don't think your direction code is right. You are treating distance as an angle. eulerAngles refers to degrees x/y/z, not a direction vector. To use a direction vector, you would assign transform.forward = direction;. What direction exactly are you trying to look? Toward something? Same (arbitrary) direction every frame? transform.LookAt() and transform.rotation = storedRotation are easy solutions for those problems.

Edit: Just to be perfectly clear, I think having the camera attached to a character is a bad idea. You will probably encounter problems with jerky movement and incorrect positions at the very least.