Comments and answers for "Head and body rotate threshold"
http://answers.unity.com/questions/1489206/head-and-body-rotate-threshold.html
The latest comments and answers for the question "Head and body rotate threshold"Comment by Dorscherl on Dorscherl's comment
http://answers.unity.com/comments/1490052/view.html
Since I'm somewhat new, I've never collaborated on any projects before so it's nerve racking lol. I'm interested in it as I know I'll need help. Exchange contact info? Discord? Email?Thu, 05 Apr 2018 18:39:19 GMTDorscherlComment by Cornelis-de-Jager on Cornelis-de-Jager's comment
http://answers.unity.com/comments/1489691/view.html
Also really didn't know you could od that. I just learned something as well :) But as mentioned previously, I would be keen to colabWed, 04 Apr 2018 22:28:36 GMTCornelis-de-JagerComment by Cornelis-de-Jager on Cornelis-de-Jager's comment
http://answers.unity.com/comments/1489690/view.html
Great! I was jsut about to read what you did over nightWed, 04 Apr 2018 22:25:47 GMTCornelis-de-JagerComment by Dorscherl on Dorscherl's comment
http://answers.unity.com/comments/1489683/view.html
I got it working! I was tinkering with your answer and I started to remember how Unity Standard Assets uses an Animation Curve as a slope modifier. I looked into how to use an animation curve for what I'm doing, I had to change the x and y values to work though. (x) being the angle difference and (y) being the max angle. The good thing about doing it this way is I can change exactly how the values change.
<br>Here's a screenshot of the animation curve:
![alt text][1]
<br>Here is the new code:
[SerializeField] private AnimationCurve angleCurve;
private float CalculateMaxAngle()
{
Vector3 lookDir = head.forward;
Vector3 faceDir = body.forward;//body.forward;
float diff = Mathf.Abs(Vector3.Angle(faceDir, lookDir)) * 2f;
Debug.Log("Diff: " + diff);
//write the curve function
float angle = LookCurve(diff);
Debug.Log("Angle: " + angle);
return angle;
}
private float LookCurve(float diff)
{
//linear a diamond shape lookbox
//return 90 - diff;
//cats eye (my diagram)
return angleCurve.Evaluate(diff);
//return 90 * Mathf.Pow(diff / 90, 2.5f);
}
[1]: /storage/temp/114276-animation-curve.pngWed, 04 Apr 2018 22:19:52 GMTDorscherlComment by Dorscherl on Dorscherl's comment
http://answers.unity.com/comments/1489553/view.html
Thank you. Here's another video with the updated script: https://youtu.be/u5Rkyn_$$anonymous$$AaE. The diff and angle start at zero and angle increases with diff as it looks up/down :/
I am starting to understand what's happening now and realize i don't need the ellipse :] phew
/* What I need
As diff gets bigger, angle gets smaller
As diff gets smaller, angle gets small
Angle is maxed at whatever value (say 90 for simplicity)
*/
As for collaborating on the project, this is something I'm doing in my spare time as a way to get my hands dirty and learn, also I'm not quite sure where I'm going with this project. That said, I probably will need help like this again so i would be interested in collaboration on a small scale like bug-fixing and help like this.Wed, 04 Apr 2018 15:41:16 GMTDorscherlComment by Cornelis-de-Jager on Cornelis-de-Jager's comment
http://answers.unity.com/comments/1489320/view.html
Thats a very nice video btw, the issue here is the faceDirection. When you start off the diff should be 0. Ins$$anonymous$$d, it is 180.
So try this:
// replace this
Vector3 faceDir = new Vector3(0f, body.position.y, 0f);
// with this:
Vector3 faceDir = body.forward;
BTW if you would like to colab on that project I would be keen. I probally will only be helping out with bug fixing and stuff like this from time to time.Wed, 04 Apr 2018 05:47:50 GMTCornelis-de-JagerComment by Dorscherl on Dorscherl's comment
http://answers.unity.com/comments/1489313/view.html
Here's a brief demo of it running: https://youtu.be/4hjUJdbVdp4
I said in the description of what was happening. Basically, I would look down and the angle set by LoopCurve function would be 0 (awesome) but then i look up and the value sky rockets to like 3000 and makes checking the left/right angle pointless. I'll give you what I have that determines if the character can rotate or not.
private void RotateBody()
{
//Get the direction relative to the body
Vector3 faceDirection = body.InverseTransformDirection(head.forward);
//Get the angle between the facing direction and body forward
float angle = Mathf.Atan2(faceDirection.x, faceDirection.z) * Mathf.Rad2Deg;
//Set the target rotation speed whether or not the player is moving
//if (moveInput == Vector3.zero)
// targetRotateSpeed = 0.0f;
//else
// targetRotateSpeed = 7.0f;
//Set the rotation speed to smoothly translate to the target
rotateSpeed = Mathf.Lerp(rotateSpeed, targetRotateSpeed, Time.fixedDeltaTime * rotateSpeedDamp);
//calculate roation to be applied
float targetRotation = angle * Time.deltaTime * rotateSpeed;
if (angle > maxAngle) targetRotation = Mathf.Clamp(targetRotation, angle - maxAngle, targetRotation);
if (angle < -maxAngle) targetRotation = Mathf.Clamp(targetRotation, targetRotation, angle + maxAngle);
//finally, rotate the body
rigidbody.MoveRotation(Quaternion.AngleAxis(targetRotation, body.up) * rigidbody.rotation);
}Wed, 04 Apr 2018 05:19:13 GMTDorscherlComment by Cornelis-de-Jager on Cornelis-de-Jager's comment
http://answers.unity.com/comments/1489309/view.html
Its true that the curve goes on Infinitely, however, since the max turning angle is 90 this should not be a problem. What is it currently doing in Unity?Wed, 04 Apr 2018 05:02:44 GMTCornelis-de-JagerComment by Dorscherl on Dorscherl's comment
http://answers.unity.com/comments/1489303/view.html
I am very close to a solution with this, the line curve is does not achieve the "cat's eye" as you called it. It goes on infinitely...![alt text][1]
A semi-ellipse or semi-circle would probably achieve the cat's eye. The semi-circle would need to have a max x value of whatever I want it to be (say 80). I just don't know how to achieve that.
[1]: /storage/temp/114240-line-curve.pngWed, 04 Apr 2018 04:43:30 GMTDorscherlComment by Cornelis-de-Jager on Cornelis-de-Jager's comment
http://answers.unity.com/comments/1489288/view.html
Ran through the Math Again. There are two mistakes in there:
// Change this
float diff = Mathf.Abs(Vector3.Angle(lookDir, faceDir)) * 2f;
// To this:
float diff = Mathf.Abs(Vector3.Angle(lookDir, faceDir));
AND
// Change this:
return 90 * Mathf.Pow(diff / 90, 2.5f);
// To this
return 90 * (1 - Mathf.Pow(diff / 90, 2.5f));
I Also updated the answerWed, 04 Apr 2018 03:21:46 GMTCornelis-de-JagerComment by Dorscherl on Dorscherl's answer
http://answers.unity.com/comments/1489281/view.html
I'm trying it out now and am noticing some things. I get a error with using Vector3s within a Quaternion.Angle() method, I switched it to Vector3.Angle and will give it a shot. I am also noticing an issue of using the power in the LookCurve(float diff) method, I rewrote it using Mathf.Pow(diff/90, 2.5).
A way to draw this in the inspector would be nice, but google does show the curve I want to have. I assume 90 can be replaced with whatever number I have set for the clamped angle at which the player can look up and down?
And to know what values your using for lookDir and faceDir, lookDir is "head" and faceDir is the "body". Please correct my errors in understanding
EDIT:: <br>
I tested it out but I had to change the faceDir to accept the body's y position, and lookDir to be the head.forward to see a change in maxAngle. It works however the maxAngle gets into the thousands range when looking up (0 at bottom). I need a curve that when looking down or up the value is 0, and when looking straight is the max number is like 80.<br>
Here what I did:
private float CalculateMaxAngle()
{
Vector3 lookDir = head.forward; //new Vector3(0, head.rotation.y, 0);
Vector3 faceDir = new Vector3(0f, body.position.y, 0f); //.new Vector3(0, body.forward.y, 0);
float diff = Mathf.Abs(Vector3.Angle(lookDir, faceDir)) * 2f;
Debug.Log(diff);
//write the curve function
float angle = LookCurve(diff);
return angle;
}
private float LookCurve(float diff)
{
//linear a diamond shape lookbox
//return 90 - diff;
//cats eye (my diagram)
return 90 * Mathf.Pow(diff / 90, 2.5f);
//return 90 * (1 - (diff / 90)^2.5);
}Wed, 04 Apr 2018 02:17:58 GMTDorscherlAnswer by Cornelis-de-Jager
http://answers.unity.com/answers/1489229/view.html
Ok so here is my solution. first you need to calculate the max angle you can rotate on. This needs to be dynamic:
void Update () {
maxAngle = CalculateAngle ();
}
float CalculateAngle () {
Vector3 lookDir = head.forward;
Vector3 faceDir = body.forward;
float diff = Mathf.Abs(Vector3.Angle(lookDir, faceDir));
Debug.Log(diff);
// Now write the curve function
float angle = LookCurve (diff) ;
return angle;
}
float LookCurve (float diff) {
// Cats Eye (like in diagram)
return 90 * (1 - Mathf.Pow(diff / 90, 2.5f));
}
The lookCurve function is basically what gives the Shape. For the linear it will simply be a diamond shape lookbox, for your more catlike one it will be the second return function. pick one or make your own. go to google and plug in this formula 'y = 90 * (1-(x/90)^2.5)' it will automaticall create a graph, you can then just tweek it, I suggest tweeking the power.
The next step is to Create a function that simple moves the players head towards the centre if they are out of bounds. You will need to do this yourself. Should be simple though.Tue, 03 Apr 2018 23:26:15 GMTCornelis-de-Jager