Interracting with player model (Events)

Hello,

I would like to know what system should I use in my small survival project. I’ve already created player with FPP. Now it is time to create some events. I wanted my player could for example click bush and some event happend. I’ve tried with OnTrigger, but it was working far away (for exmaple you could pick up some logs from 100 meters). Should I add some coditions and ontrigger is good method or should I use raycasting? I think raycasting is better for FPS not for survival game.

I’ve also don’t know how to check if players is nearby object, watched few youtube videos but i didn’t find any what could help me.

Thank you,

It really depends.

If you have a giant lava lake, then you want some kind of collider on the lava, as well as some way to say “this collider will kill the player” or at least roast him.

If you are talking about picking up a log, raycast is always better, because then if there are 2 logs stacked right next to each other, you can get exactly the one you need.

Remember raycast can also take a length. This keeps it from going infinitely across the map.

Use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:

1 Like

Now I used Raycast and for the first argument I used position as my cursor from camera (FPP).

On the method Start() I used (ray = cam.ScreenPointToRay(Input.mousePosition):wink: and in update method i’ve got

    RaycastHit hit;
    Ray ray;

    Camera cam = GameObject.Find("Player").GetComponent<Camera>();
 public void Awake()
    {
        ray = cam.ScreenPointToRay(Input.mousePosition);
    }
void Update(){
if(isInRange)
        {
            if(Physics.Raycast(ray, out hit, 5f))
            {
                if(hit.transform.tag == "Cube")
                {
                    Debug.Log("Cube hit.");
                }
            }
        }
{

Object reference not set to an instance of an object
CubeEvent.Awake () (at Assets/CubeEvent.cs:41)

When you see this error, know that it DOES NOT MATTER what you are doing. It makes no difference.

Why? Because the answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7