- Home /
Calling a function from FixedUpdate by collision with an object
So I m trying to make a script,if the shuriken hits the Katana,then it should act like a homing missile and hit the enemy,and I dont know how to call the homing missile function after the collision was made with the katana,pls help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lerp : MonoBehaviour {
public Transform shurikenTarget;
public Rigidbody shurikenRb;
public float turn;
public float rocketVelocity;
private void FixedUpdate()
{
MoveToTarget();
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Katana")
{
// if I insert here MoveToTarget it doesent work :(
}
}
void MoveToTarget()
{
shurikenRb.velocity = transform.forward * rocketVelocity;
var rocketTargetRotation = Quaternion.LookRotation(shurikenTarget.position - transform.position);
shurikenRb.MoveRotation(Quaternion.RotateTowards(transform.rotation, rocketTargetRotation, turn));
}
}
Is this script attached to one of the objects that is colliding? 2. Are you sure you set up the tags for the objects? The code is working fine for me when i'm testing it with 2 simple cubes, with one of the cubes having the OnCollisionEnter function.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I don't think I get collision box, can someone help? 0 Answers
Can you help solve the following error? 1 Answer
C# OnCollisionEnter2D Not Occuring 1 Answer