- Home /
Video provided-Rotate 2d character in 3d world
video link https://www.youtube.com/watch?v=KMfmu943-m0&feature=youtu.be
Hello. I am trying to rotate a 2d plane relative to the camera depending on the cam's position & rotation to it.
the 2d plan is a character in a 3d world. Of course when the cam comes to or is at the left/right of the plane, the plan will seem to disappear. I am trying to get the character plane to rotate at least 30 degrees at that point towards the left/right depending on the cam position, so it will never 'disappear'.
the closest i came to this was in this attempt
var checks : float[];
var myRots : float[];
if(Mathf.Clamp(cam.transform.eulerAngles.y, checks[i]-5, checks[i] + 25) == cam.transform.eulerAngles.y)
transform.localEulerAngles.y = transform.parent.transform.rotation.y * myRots[i];
Here i manually input camera rotations into the checks string, and when the cam rotation is that rotation, i tell the character to rotate itself accordingly.
this only works until the character itself rotates, then the everything i set becomes incorrect. The character is in an empty gameObject. I rotate the parent obj when moving in the world, and rotate the actual plan as needed for the camera.
Please give an example when explaining- I am horrible at rotations! Thanks
Answer by robertbu · Oct 26, 2014 at 04:02 PM
While I believe I understand your problem, without more of your code and an understanding of the app, I'm unsure of the best fix. Here is my suggestion. I'm assuming you are using Unity's Plane when you write 'plane'. Instead, use a Unity Quad. The geometry is simpler, and it 'faces' the camera by default.
Each frame you get the Vector3.Angle() between the camera forward and the Quad forward. If they greater than 90, then the Quad cannot be seen. So for your 30 degrees check, you would use 60 degrees. If the angle is greater than 60 degrees, then apply the same rotation to the Quad you did to the camera. Untested, but something like this would work:
#pragma strict
var prevCamForward : Vector3;
var camTrans : Transform;
var qPrev : Quaternion;
function Start() {
camTrans = camera.main.transform;
prevCamForward = camTrans.forward;
qPrev = camTrans.rotation;
}
function LateUpdate() {
if (prevCamForward != camTrans.forward) {
if (Vector3.Angle(transform.forward, camTrans.forward) > 60.0) {
transform.rotation = camTrans.rotation * Quaternion.Inverse(qPrev) * transform.rotation;
}
}
prevCamForward = camTrans.forward;
qPrev = camTrans.rotation;
}
I tried this but it just behaves like transform.lookAt(cam.transform.position). I provided a video to clarify the question.
Hello? I seems logical but does not work. Am i missing something?
@superventure - I now understand your problem, but in a quick try I was unable to provide code for an alternate fix. The code above assures that the front side always faces the camera (so you cannot roll around to the back side), but that is not what you want (based on the video). I'll give it another look when I'm back at my desktop and have a bit more time. $$anonymous$$y solution worked for the front side, but not for the back side. The idea was that, when the angle was below some threshold, to rotate the object about an axis create by the Vector3.Cross() of the camera forward and the object forward to flip the object to the opposite side.
Awesome! I know there has to be some way to simply it a little more before it faces to camera directly. I just cant put my finger on it
Your answer
Follow this Question
Related Questions
Rotate object in 90 degrees relative to camera. 1 Answer
Position from rotation problem. 1 Answer
Set rotation on y-axis with angle relative to current position 0 Answers
Object makes random jumps in rotation 1 Answer
Instantiate gun & arms rig relative to rotation & position of first person controller camera 0 Answers