I am attempting to have my character’s arm follow the player’s mouse. I have two problems.
First is I cannot figure out how to lessen the distance the player’s mouse must be from the character’s arm for the character’s arm to copy its movement. That is not the most clearly worded description so I’ve attached a link to a video that demonstrates the problem.
Second is that I also have added a Math.Clamp min and max so the player cannot windmill the character’s arm but I have not figured out how to stop the arm from snapping to the max rotation when it approaches the min rotation. instead of just stopping rotating and staying at “0” degrees it snaps to 90. Thanks in advance for your time.
Below I also have attached the mouse follow script and the script that defines the mouse the “Game Cursor” Script.
ttps://www.youtube.com/watch?v=MIcUjkDGIRY
Thankyou for your time!`
public static class GameCursor
{
public static Vector2 WorldPosition
{
get
{
float distance;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (_gamePlane.Raycast(ray, out distance))
return ray.GetPoint(distance);
// Ray did not hit plane
return Vector2.zero;
}
}
private static Plane _gamePlane = new Plane(Vector3.forward, 0);
}
using UnityEngine;
public class PointAtCursor : MonoBehaviour
{
public Vector3 targetPosition;
public float maxLength;
public GameObject ArmR;
void Start ()
{
}
private void Update()
{
if (Input.GetKey (KeyCode.E)) {
Follow ();
}
if (Input.GetKeyUp (KeyCode.E)) {
}
print (transform.eulerAngles.z);
Vector3 clampedRotation = transform.eulerAngles;
// Clamp the z value
clampedRotation.z = Mathf.Clamp(clampedRotation.z, -0 ,90);
// assign the clamped rotation
ArmR.transform.rotation = Quaternion.Euler(clampedRotation);
if(ArmR.transform.eulerAngles.z == 0){
Debug.Log ("AT0");
}
if(ArmR.transform.eulerAngles.z == 90){
Debug.Log ("AT90");
}
}
void Follow(){
ArmR.transform.up = -GameCursor.WorldPosition;
}
private void OnDrawGizmos()
{
Gizmos.DrawIcon(GameCursor.WorldPosition, "wallll", true);
}
}