1
votes

I stumbled across a problem while working with the book "Unity in Action". At the end of chapter 3 you'd end up with the basics for a simple fps game. It's basically a player (camera attached to it) in a simple and small level which only exists out of a number of cubes forming walls and the floor etc. These cubes all have box collider on them. Now the player is also able to shoot at moving enemies which are also able to shoot. This was done by Raycast/RaycastHit. All this worked perfectly so I wanted to add something that reassembles bullet holes, simply by instantiating a black sphere object on the wall where the fireball (this is the object the enemy and the player shoot) hits it. This works BUT sometimes the fireball object just goes through the wall. In case there is another wall behind it, the fireball object gets destroyed by this second wall and the desired sphere is created on the second wall instead of on the first wall.

I changed the speed in the fireball from 20 to 10 and then back to 20 and at a speed of 10, the success rate is around 19/20, while at a speed of 20 it's around 6/10.

Here is the code of the fireball which is supposed to check whether it hits the player (then health is deducted, that works fine) or hits the enemy (then the enemy falls over, also works fine) or hits the wall in which case the sphere should be created.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Fireball : MonoBehaviour {

 public float speed = 10.0f;
 public int damage = 1;
 [SerializeField] private GameObject wallHit;
 private GameObject _wallHit;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {
     transform.Translate(0,0, speed * Time.deltaTime);
 }

 void OnTriggerEnter(Collider other){
     RaycastHit hit;
     PlayerCharacter player = other.GetComponent<PlayerCharacter>();
     ReactiveTarget target = other.GetComponent<ReactiveTarget>();
     WallBehavior wall = other.GetComponent<WallBehavior>();

     if(player != null){
         player.Hurt(damage);
     }
     if(target != null){
         target.ReactToHit();
     }
     if(wall != null){
         if(Physics.Raycast(transform.position, transform.forward, out hit)){
             wall.WallWasHit(hit);
         }
     }
     Destroy(this.gameObject);
 }
}

As you can see one thing I tried was to give each wall a WallBehavior script, which looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 public class WallBehavior : MonoBehaviour {
 [SerializeField] private GameObject wallHit;
 private GameObject _wallHit;
 static int count = 0;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

 }
 public void WallWasHit(RaycastHit hit){
             count++;
             Debug.Log("Wall was hit: " + count);

             _wallHit = Instantiate(wallHit) as GameObject;
             _wallHit.transform.position = hit.point;
 }
 }

But none of my attempts to understand why this happens so infrequently was successful so far and I hope someone can help me with this because I feel like might be crucial for my learning before I continue with the book! Thanks in advance. if further information are needed, I am happy to provide them. See the following picture for a better visualization of the issue. Six spheres on the wall, one fireball still in the air

EDIT: As mentioned in the answers, I replaced the fireball script accordingly but I am not sure how to change the following script likewise. This script is on my camera which is on my player object. In the Update function the Fireball is instantiated and also given a movement, so I guess this causes the problems?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayShooter : MonoBehaviour {
private Camera _camera;

[SerializeField] private GameObject fireballPrefab;
private GameObject _fireball;


void Start () {
    _camera = GetComponent<Camera>();

    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

void OnGUI(){
    int size = 12;
    float posX = _camera.pixelWidth/2 - size/4;
    float posY = _camera.pixelHeight/2 - size/2;
    GUI.Label(new Rect(posX, posY, size, size), "X");
}

// Update is called once per frame
void Update () {
    if(Input.GetMouseButtonDown(0)){
        Vector3 point = new Vector3(_camera.pixelWidth/2, _camera.pixelHeight/2, 0);
        _fireball = Instantiate(fireballPrefab) as GameObject;
        _fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
        _fireball.transform.rotation = transform.rotation;

        Ray ray2 = _camera.ScreenPointToRay(point);
        RaycastHit hit;
        if(Physics.Raycast(ray2, out hit)){
            GameObject hitObject = hit.transform.gameObject;
            ReactiveTarget target = hitObject.GetComponent<ReactiveTarget>();
            if(target !=null){
                target.ReactToHit();
            } 
        }
    }
}
}
1

1 Answers

3
votes

This is not how to move a Rigidbody Object and moving it like this could cause so many issues including the one mentioned in your question. Objects with Rigidbody should be moved with the Rigidbody component and with the functions like Rigidbody.MovePosition, Rigidbody.AddForce and Rigidbody.velocity not by the transform or transform.Translate.

Also, you should move the Rigidbody Object in the FixedUpdate function instead of the Update function. If you are moving your other Rigidbody Objects by their transform then you must fix them too. The example below replaces transform.Translate with Rigidbody.MovePosition in the Fireball script:

public float speed = 10.0f;
public int damage = 1;
[SerializeField]
private GameObject wallHit;
private GameObject _wallHit;

public Rigidbody rb;

// Use this for initialization
void Start()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void FixedUpdate()
{
    //Move to towards Z-axis
    Vector3 pos = new Vector3(0, 0, 1);
    pos = pos.normalized * speed * Time.deltaTime;

    rb.MovePosition(rb.transform.position + pos);
}

void OnTriggerEnter(Collider other)
{
    RaycastHit hit;
    PlayerCharacter player = other.GetComponent<PlayerCharacter>();
    ReactiveTarget target = other.GetComponent<ReactiveTarget>();
    WallBehavior wall = other.GetComponent<WallBehavior>();

    if (player != null)
    {
        player.Hurt(damage);
    }
    if (target != null)
    {
        target.ReactToHit();
    }
    if (wall != null)
    {
        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            wall.WallWasHit(hit);
        }
    }
    Destroy(this.gameObject);
}

If you still run into issues, use Rigidbody.velocity instead:

void FixedUpdate()
{
    Vector3 pos = Vector3.zero;
    pos.z = speed * Time.deltaTime;

    rb.velocity = pos;
}

Sometimes, depending on the size of the object and the speed it is moving by, you many need to set its Rigidbody Interpolate from None to Interpolate and Collision Detection to Continuous.

Don't forget to remove the code in the Update function.


EDIT:

I looked at your project and found new problems:

1.You enabled IsTrigger on your "Fireball" GameObject. Please uncheck the IsTrigger on the collider.

2.You just want to shoot a bullet. The force should be added once only not every FixedUpdate. Add the force in the Start function instead of the FixedUpdate function. Use Rigidbody.velocity instead of Rigidbody.MovePosition.

Remove the FixedUpdate function and the code inside it.

This should be your new Start function:

void Start()
{
    rb = GetComponent<Rigidbody>();

    Vector3 pos = transform.forward * speed * Time.deltaTime;
    rb.velocity = pos;
}