Skip to main content
Typo
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

You seem to fundamentally misunderstand how raycasts work. It's not a signal that pokes the object that it hits into action. It's a sensor that reports back to the one firing it what it hit.

It is the responsibility of the code that fired the ray to take the next action — objects hit by the ray have no idea they were hit by anything at all unless you write code to tell them so.

You likely want to do something a bit more like this:

// First, define an interface for all objects usable this way.
public interface IPlayerUsable {
    void Use();
}

// Implement that interface on each object's script.
public class PerspectiveChange : MonoBehaviour, IUsableIPlayerUsable {
    public Camera _camera;
    bool beingUsed;
    
    void Use() {
        beingUsed = true;
        _camera.gameObject.SetActive(true);
        Player.ganeObject.SetActive(false);
    }
}

// Now your ray routine can look something like this:
void Update() {
    if (Input.GetButtonDown("Use")) {
        RaycastHit hit; 
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

        if (Physics.Raycast(ray, out hit, 10.0f)) {
            if (hit.collider.TryGetComponent(out IPlayerUsable usable)) {
                usable.Use();
            }
        }
    }
}

You seem to fundamentally misunderstand how raycasts work. It's not a signal that pokes the object that it hits into action. It's a sensor that reports back to the one firing it what it hit.

It is the responsibility of the code that fired the ray to take the next action — objects hit by the ray have no idea they were hit by anything at all unless you write code to tell them so.

You likely want to do something a bit more like this:

// First, define an interface for all objects usable this way.
public interface IPlayerUsable {
    void Use();
}

// Implement that interface on each object's script.
public class PerspectiveChange : MonoBehaviour, IUsable {
    public Camera _camera;
    bool beingUsed;
    
    void Use() {
        beingUsed = true;
        _camera.gameObject.SetActive(true);
        Player.ganeObject.SetActive(false);
    }
}

// Now your ray routine can look something like this:
void Update() {
    if (Input.GetButtonDown("Use")) {
        RaycastHit hit; 
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

        if (Physics.Raycast(ray, out hit, 10.0f)) {
            if (hit.collider.TryGetComponent(out IPlayerUsable usable)) {
                usable.Use();
            }
        }
    }
}

You seem to fundamentally misunderstand how raycasts work. It's not a signal that pokes the object that it hits into action. It's a sensor that reports back to the one firing it what it hit.

It is the responsibility of the code that fired the ray to take the next action — objects hit by the ray have no idea they were hit by anything at all unless you write code to tell them so.

You likely want to do something a bit more like this:

// First, define an interface for all objects usable this way.
public interface IPlayerUsable {
    void Use();
}

// Implement that interface on each object's script.
public class PerspectiveChange : MonoBehaviour, IPlayerUsable {
    public Camera _camera;
    bool beingUsed;
    
    void Use() {
        beingUsed = true;
        _camera.gameObject.SetActive(true);
        Player.ganeObject.SetActive(false);
    }
}

// Now your ray routine can look something like this:
void Update() {
    if (Input.GetButtonDown("Use")) {
        RaycastHit hit; 
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

        if (Physics.Raycast(ray, out hit, 10.0f)) {
            if (hit.collider.TryGetComponent(out IPlayerUsable usable)) {
                usable.Use();
            }
        }
    }
}
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

You seem to fundamentally misunderstand how raycasts work. It's not a signal that pokes the object that it hits into action. It's a sensor that reports back to the one firing it what it hit.

It is the responsibility of the code that fired the ray to take the next action — objects hit by the ray have no idea they were hit by anything at all unless you write code to tell them so.

You likely want to do something a bit more like this:

// First, define an interface for all objects usable this way.
public interface IPlayerUsable {
    void Use();
}

// Implement that interface on each object's script.
public class PerspectiveChange : MonoBehaviour, IUsable {
    public Camera _camera;
    bool beingUsed;
    
    void Use() {
        beingUsed = true;
        _camera.gameObject.SetActive(true);
        Player.ganeObject.SetActive(false);
    }
}

// Now your ray routine can look something like this:
void Update() {
    if (Input.GetButtonDown("Use")) {
        RaycastHit hit; 
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

        if (Physics.Raycast(ray, out hit, 10.0f)) {
            if (hit.collider.TryGetComponent(out IPlayerUsable usable)) {
                usable.Use();
            }
        }
    }
}