- Home /
Raycast not working on object without gravity
Hi! I am making a simple shooter scene where I have a weapon that makes enemies float and a normal gun. When just moving I can shoot the enemy and everything works fine. When the enemy is of the ground when hit by the other gun (setting the using gravity on the rigidbody to false) it is impossible to hit it. I did find out it doesn't hit it because it doesn't trigger the getcomponent function for getting the script of the enemy which it does when the enemy is grounded. Any help would be greatly appreciated!
Part of script that gets the hit :
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
if (Input.GetMouseButton(0) && cooldown <= 0)
{
Rigidbody clone;
clone = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
Debug.DrawRay(transform.position, fwd * 10);
cooldown = 0.3f;
if (Physics.Raycast(transform.position, fwd, out hit))
{
Debug.Log("Hit!");
aibehav = hit.transform.gameObject.GetComponent<AIbehav>();
aibehav.getDamage(damage);
print("something is hit!" + hit);
}
}
}
enemy script
public float Health;
UnityEngine.AI.NavMeshAgent nav;
GameObject player;
Vector3 playerpos;
public float delay = 0;
bool Up = false;
public bool mayNav = true;
// Use this for initialization
void Awake()
{
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
player = GameObject.FindGameObjectWithTag("Player");
}
void Start()
{
Health = 100;
}
// Update is called once per frame
void Update()
{
if (Health > 0 && nav.enabled == true)
{
playerpos = player.transform.position;
nav.SetDestination(playerpos);
}
if (Health <= 0)
{
Destroy(gameObject);
}
delay -= 1 * Time.deltaTime;
}
public void getDamage(float damage)
{
Health -= damage;
}
}
gravity disable script
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
AIbehav aibehav = other.gameObject.GetComponent<AIbehav>();
NavMeshAgent NVM = other.GetComponent<NavMeshAgent>();
Rigidbody RB = other.GetComponent<Rigidbody>();
RB.useGravity = false;
NVM.enabled = false;
RB.mass = -5.0f;
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
[c#] Hit a object and shoot it away with raycast? 2 Answers
Does Physics.Raycast(All) support/use Acceleration Structures internally? 0 Answers
(C#) Collider to collider2d 0 Answers
Can't get a laser working properly. 2 Answers