I’m trying to write a simple script to add force to an enemy character. I’d like to get the raycast to come from a gun object, but I made it go from the camera for now. The problem is that it does not add force to the enemy character. It adds force to a cube I placed in the scene as a test, but not the enemy character. The character has a rigidbody, and I’ve tried putting a mesh collider and a box collider on it. None of them work. Also how would I get it to cast from a game object’s transform? Here is my current code:
using UnityEngine;
using System.Collections;
public class RaycastShot : MonoBehaviour {
public float range;
public float power;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray sniperShot = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(sniperShot, out hitInfo, range))
{
Debug.Log("You are shooting");
Debug.DrawLine(sniperShot.direction, hitInfo.point);
if (hitInfo.rigidbody != null)
{
hitInfo.rigidbody.AddForceAtPosition(sniperShot.direction * power,hitInfo.point);
}
}
}
}
}