Forum rules - please read before posting.

FollowMouse / GetMousePosition - occasional random Error

Hi all.

I have an object which follows the mouse around the screen. I use this feature many scenes, many different uses. It's a simple code script added onto objects.

It works much of the time.... It works 100% if I dont change scenes in Unity. But if a scene change has occurred, (including reloading a scene if already loaded etc) the object following mouse is sometimes offset a little, other times lots, and so is displayed off screen.

Here is the code. This happens in builds and in editor just the same.

using UnityEngine;
using System.Collections;

public class FollowMouse : MonoBehaviour
{

public float mydistance; // Set distance  the object is placed in front of the camera.

void Update () {

    Vector3 temp = (AC.KickStarter.playerInput.GetMousePosition());
    temp.z = mydistance; // distance the object is placed in front of the camera.
    this.transform.position = Camera.main.ScreenToWorldPoint(temp);
 }
}

Is it reading the AC.KickStarter.playerInput.GetMousePosition wrong occasionally or do I need some way to check the AC Mouse position is correctly read after a scene change?

Comments

  • Found a work around but not a fix.

    My object following mouse contains nested objects, those have 'movable' and 'remember movable' AC scripts attached for puzzle usage. If I remove the “remember movable” the code to follow mouse works after scene changes.

    I have no idea why this is happening, but can work around it.

  • Yes - it sounds like a child offset issue, rather than a mousep position one. GetMousePosition should be consistent across all scenes.

    The Remember Moveable will update the object's position when loading a scene. If you needed to reset it's position but still make use of this component, you could try hooking into the OnAfterChangeScene to give it a correct local position, but that would depend on how your hierarchy is arranged.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.