Thursday, September 29, 2022
HomeGame Developmentc# - LookRotation/object roll challenge in Unity

c# – LookRotation/object roll challenge in Unity


I hoped to get some assist with a LookRotation() challenge I have been having in Unity/C#.

In my challenge I’ve a goal that attaches to the digital camera when clicked on (consider it like grabbing an object off a shelf together with your hand and pulling it shut). Whereas the item is being pulled in direction of the digital camera it wants to stay dealing with the digital camera always together with when the digital camera itself rotates; both horizontally or vertically. The item may also be dragged on display with the mouse.

To make sure the item faces the digital camera I mainly use the next code:

Vector3 path = (goal.remodel.place - Digital camera.important.remodel.place).normalized;
Quaternion lookTarget = Quaternion.LookRotation(path, Vector3.up);
goal.remodel.rotation = lookTarget;

Whereas this may be sure that the item stays dealing with 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 even have elevated roll. The larger the digital camera rotation is in x, the larger the goal will roll.

Within the picture under I’ve grabbed the left object and hooked up it to the left aspect of the display; notice the roll launched within the 2nd picture in contrast with the primary. The rotation of the item in picture 1 is what I wish to replicate in picture 2.

screenshot demonstrating problem

I notice that the next code can appropriately modify the roll as desired (10 levels on this case):

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

Nevertheless, I am not sure how a lot to regulate the roll by because the roll quantity will fluctuate primarily based on the digital camera’s rotation in x. I am undecided what the connection is between the 2 or the best 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 could be nice to hopefully perceive this higher and resolve the problem.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments