Detecting Collision is not working
I Am trying to move the ball when the key "I" is pressed when making contact with the players, this is my code which is not working
using UnityEngine;
using System.Collections;
public class KickForce : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col) {
if(col.gameObject.tag == "Player")
{
if (Input.GetKey ("i")) {
transform.Translate(Vector3.right * Time.deltaTime * 10);
}
}
}
}
In theory, I see nothing wrong with your code. It's probably a setup problem. Add Debug.Log's to see, if OnCollisionEnter is getting called at all. Are you working in 2D? Does the player or the ball have a rigidbody?
i did use the Debug.Log, I Do not think its working to call the OnCollisionEnter and both player and the ball have a rigidbody
Are you working in 2D? How are you moving the player or ball?
check in inspector for both of object istrigger must be off
Answer by Oribow · Apr 11, 2016 at 12:19 PM
"The biggest difference between manipulating the Transform versus the Rigidbody is the use of forces. Rigidbodies can receive forces and torque, but Transforms cannot. Transforms can be translated and rotated, but this is not the same as using physics. You’ll notice the distinct difference when you try it for yourself. Adding forces/torque to the Rigidbody will actually change the object’s position and rotation of the Transform component. This is why you should only be using one or the other. Changing the Transform while using physics could cause problems with collisions and other calculations." (http://docs.unity3d.com/Manual/class-Rigidbody.html)
That is your problem. You should not move rigidbody wit transform.Translate(Vector3.right * Time.deltaTime * 10);
Use Forces, or now collision will be detecteable.
i have used the Rigidbody add force but still, i think its the OnCollisionEnter which is not working
Here is the script
using UnityEngine;
using System.Collections;
public class $$anonymous$$ickForce : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col) {
if(col.gameObject.tag == "Player")
{
if (Input.Get$$anonymous$$ey ("i")) {
GetComponent<Rigidbody>().AddForce (transform.right * 20);
}
}
}
}
i am currently using transform for the player's movement which works perfectly
Do you think this would have the same effect on Animations?