Home Game Development c# – LookRotation/object roll situation in Unity

c# – LookRotation/object roll situation in Unity

0
c# – LookRotation/object roll situation in Unity

[ad_1]

I hoped to get some assist with a LookRotation() situation I’ve been having in Unity/C#.

In my undertaking I’ve a goal that attaches to the digital camera when clicked on (consider it like grabbing an object off a shelf along with your hand and pulling it shut). While the article is being pulled in the direction of the digital camera it wants to stay going through the digital camera always together with when the digital camera itself rotates; both horizontally or vertically. The object can be dragged on display screen with the mouse.

To guarantee the article faces the digital camera I mainly use the next code:

Vector3 route = (goal.remodel.place - Camera.foremost.remodel.place).normalized;
Quaternion lookTarget = Quaternion.LookRotation(route, Vector3.up);
goal.remodel.rotation = lookTarget;

While it will make sure that the article stays going through the digital camera it additionally causes the goal to roll in its ahead axis when the digital camera rotates up or down. If the goal is grabbed whereas the digital camera x rotation is off centre, the goal may also have elevated roll. The higher the digital camera rotation is in x, the higher the goal will roll.

In the picture beneath I’ve grabbed the left object and hooked up it to the left facet of the display screen; word the roll launched within the 2nd picture in contrast with the primary. The rotation of the article in picture 1 is what I wish to replicate in picture 2.

screenshot demonstrating problem

I word that the next code can accurately regulate the roll as desired (10 levels on this case):

lookTarget *= Quaternion.AngleAxis(10f, Vector3.ahead);

However, I’m uncertain how a lot to regulate the roll by because the roll quantity will fluctuate based mostly on the digital camera’s rotation in x. I’m undecided what the connection is between the 2 or the right way to calculate it.

I’ve additionally tried different issues like multiplying the lookTarget worth by the digital camera rotation and utilizing Vector3.proper because the upwards parameter within the LookRotation() technique (works however not for various y digital camera rotation values).

Any assist can be nice to hopefully perceive this higher and resolve the difficulty.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here