Object and first-person controller's collision not being registered
Basically, I'm trying to develop a mechanic where an object will stop being affected by gravity and be pushed forward by the player when the two collide, so I wrote a C# script to do exactly that. However, whenever the player collides with the object, it doesn't seem to register a collision. Any help or advice on fixing this issue?
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectScript : MonoBehaviour {
public float force;
// Use this for initialization
void Start () {
GetComponent<Rigidbody> ().useGravity = true;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col) {
if (col.gameObject.tag == "Player") {
GetComponent<Rigidbody> ().useGravity = false;
Vector3 dir = col.contacts [0].point - transform.position;
dir = -dir.normalized;
GetComponent<Rigidbody> ().AddForce (dir * force);
}
}
}
Answer by luxon001 · Sep 10, 2017 at 12:29 PM
I have thought of only 2 things that could be causing the issue:
you are looking for the tag "Player" while the tag your player has is actually "player"
if your player doesnt have a rigidbody attached (or its set to kinematic) the collision wont be registered in OnCollisionEnter, or at least thats what i have been told.
hope this helps...
Your answer
![](https://koobas.hobune.stream/wayback/20220612130607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigid Body and Collision 1 Answer
Bugging ragdoll 1 Answer
Jump is higher then normal 0 Answers
how can I add Sprint into my Script ?,How can I add Sprint function ?? 0 Answers
Rigidbody - Why does my issue get fixed by doing THIS? 0 Answers