- Home /
Question by
conguerror · Feb 03, 2021 at 04:23 PM ·
c#rotationanglesbodyvector3.angle
Forward angles
Hello guys, I need your help. So I have a problem with checking the angles from the forward of the character. Player rotates his torso and after certain angle it should update it's forward(update feet) and check for angles again. Hope you will understand.Maybe the picture will explain it better.
capture.png
(9.9 kB)
Comment
Best Answer
Answer by conguerror · Feb 03, 2021 at 07:28 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
//mouseTestPos = Mathf.Clamp((Input.mousePosition.x / Screen.width) * 2 - 1, -1.0f, 1.0f);
// animator.SetFloat("Mouse", mouseTestPos);
public class PlayerLookAtDemo : MonoBehaviour
{
public Vector3 mousePos;
public Vector3 mouseWorldPoint;
public Vector3 direction;
public Vector3 directionOnPlane;
public Quaternion rotationOnPlane;
public float angleBetween = 0f;
public float angularThreshold = 35f;
private const float DebugLineScaler = 10f;
public void Update()
{
var mouse = Mouse.current;
mousePos = new Vector3(mouse.position.x.ReadValue(), mouse.position.y.ReadValue(),
Camera.main.transform.position.y - transform.position.y);
mouseWorldPoint = Camera.main.ScreenToWorldPoint(mousePos);
direction = (mouseWorldPoint - transform.position).normalized;
directionOnPlane = Vector3
.ProjectOnPlane(Camera.main.transform.rotation * Vector3.forward,
transform.up).normalized;
if (directionOnPlane.sqrMagnitude == 0f)
{
directionOnPlane = Vector3
.ProjectOnPlane(Camera.main.transform.rotation * Vector3.up, transform.up)
.normalized;
}
rotationOnPlane = Quaternion.LookRotation(directionOnPlane, transform.up);
var resultingVector = rotationOnPlane * direction;
angleBetween = Vector3.Angle(transform.forward, resultingVector);
Debug.DrawRay(transform.position, transform.forward * DebugLineScaler, Color.blue);
Debug.DrawRay(transform.position, resultingVector * DebugLineScaler, Color.red);
if (angleBetween > angularThreshold)
{
var newRotation = Quaternion.LookRotation(resultingVector, transform.up).eulerAngles;
transform.rotation = Quaternion.Euler(0, newRotation.y, 0);
}
else
{
float mouseTestPos = Mathf.Clamp((Input.mousePosition.x / Screen.width) * 2 - 1, -1.0f, 1.0f);
gameObject.GetComponent<Animator>().SetFloat("Mouse", mouseTestPos);
}
}
}
Answer by FlightOfOne · Feb 03, 2021 at 05:48 PM
You can do something like this. Keep in mind that this is not tested, but it should provide you a starting point.
void TurnToMouse(Transform head, Transform feet, Vector3 mouseWorldPosition, float angleLimit, bool flatten)
{
Vector3 headForward = head.forward;
Vector3 directionToMouse = mouseWorldPosition - head.position;
//flattens to x,z plane this is assuming you do not want the feet to turn.
if (flatten)
{
directionToMouse.y = 0;
headForward.y = 0;
}
if(Vector3.Angle(headForward, directionToMouse)>angleLimit)
{
feet.rotation = Quaternion.LookRotation(directionToMouse);
}
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
Random direction between maxAngle and minAangle , Quaternion issue.... 0 Answers
Distribute terrain in zones 3 Answers