- Home /
ScreenPointtoRay Creating problems for mouse follow script
Hello, I am confused as to why my game cursor follow script does not seem to perform differently based on the character's position. I want the player to press "E" if the player hovers the mouse over the character's hand and clicks and drags the arm will move up and down following the mouse position.
The script was working until I moved my character along the X axis and found that the location the mouse had to click and drag stayed at the arm's original position even as the character, his arm, and camera moved away. Would anyone have any idea why? I've pasted in the script for finding the Mouse's position and the script for having my game object copy the mouse's position.
I uploaded some videos of the script working on YouTube as well https://www.youtube.com/watch?v=grhnqckOfc0 https://www.youtube.com/watch?v=ZlLLgnU8yMc
Thanks for your time!
public static class GameCursor
{
public static LayerMask mask;
public static Vector2 WorldPosition
{
get
{
float distance;
int mask = 1<<9; //setting layer mask to layer 9
mask = ~mask; //setting the layer mask to ignore layer(~)
//adding in the layermask to raycast will ignore 9th layer
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.black);
if (_gamePlane.Raycast(ray, out distance))
return ray.GetPoint(distance);
return Vector2.zero;
}
}
private static Plane _gamePlane = new Plane(Vector3.forward,0);
}
point at cursor script----------------------------------------------------------------------------------------------
public GameObject ArmR;
public static Vector2 OffsetPoint;
public Vector2 OffsetVec2;
void Start ()
{
}
private void Update()
{
if (Input.GetKey (KeyCode.E)) {
Follow ();
}
if (Input.GetKeyUp (KeyCode.E)) {
}
//print (transform.eulerAngles.z);
//Offset.transform.position = OffsetVec2;
OffsetPoint = GameCursor.WorldPosition + OffsetVec2;
Vector3 clampedRotation = transform.eulerAngles;
// Clamp the z value
if (clampedRotation.z > 90.0f) clampedRotation.z = 180.0f - clampedRotation.z;{
clampedRotation.z = Mathf.Clamp (clampedRotation.z, 0, 90);
clampedRotation.z = Mathf.Clamp (clampedRotation.z, -90, 180);
}
// assign the clamped rotation
ArmR.transform.rotation = Quaternion.Euler(clampedRotation);
//if(ArmR.transform.eulerAngles.z >= 350 || transform.eulerAngles.z <= 270){
//Debug.Log ("no");
//}
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 + OffsetVec2;
}
private void OnDrawGizmos()
{
Gizmos.DrawIcon(GameCursor.WorldPosition, "wallll", true);
}
}
Answer by ArseneySorokin · Sep 27, 2018 at 11:21 AM
You are receiving the point on the mouse cursor is on the screen with GameCursor.WorldPosition, then you are adding some offset to it OffsetPoint = GameCursor.WorldPosition + OffsetVec2 and then you never use OffsetPoint again. I think that's the problem.
Thankyou for your anwser!! That seems to be the problem! I am still in the process of figuring out how to add the Offset Point correctly though. I assume I should add it in the Follow() method, initially I just added it to . ArmR.transform.up = -GameCursor.WorldPosition + OffsetVec2 + OffsetPoint;
and the arm is responsive to the mouse as it moves along the x axis which is great. but the mouse follow is not working as it should, probably because adding the offset point there is conflicting with the arm being able to smoothly follow the mouse. But I can't think too clearly about why that is right now.