Adding collisions to a custom character controller?
Alright, so I'm currently making a game where players control cars (3rd person view), but I can't seem to get players to collide with object colliders, let alone other players. I have written a custom character controller and apparently do not know how to implement collisions with the new script.
Here is my code:
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour {
private float speed = 9.0f;
private float boostSpeed = 1.5f;
private float rollRate = 20.0f;
private float elevatorRate = 3.0f;
private float rudderRate = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButton ("Forward"))
{
transform.Translate(-1.0f * speed * Time.deltaTime, 0, 0); //the thrust forward
}
if (Input.GetButton ("Backward"))
{
transform.Translate(speed * Time.deltaTime, 0, 0); //the thrust forward
}
if (Input.GetButton ("Boost"))
{
transform.Translate(-1.0f * boostSpeed * speed * Time.deltaTime, 0, 0);
}
if (Input.GetButton ("Rollright"))
{
transform.Rotate(rollRate * Time.deltaTime, 0, 0);
}
else if (Input.GetButton ("Rollleft"))
{
transform.Rotate(rollRate * -1.0f * Time.deltaTime, 0, 0);
}
if (Input.GetButton ("Rise"))
{
transform.Translate(0, elevatorRate * Time.deltaTime, 0);
}
else if (Input.GetButton ("Dive"))
{
transform.Translate(0, elevatorRate * -1.0f * Time.deltaTime, 0);
}
if (Input.GetButton ("Rudderright"))
{
transform.Rotate(0, rudderRate * Time.deltaTime, -.07f * Time.deltaTime);
}
else if (Input.GetButton ("Rudderleft"))
{
transform.Rotate(0, rudderRate * -1.0f * Time.deltaTime, .07f * Time.deltaTime);
}
}
}
I have colliders and a Rigidbody attached to my player prefab, with the Rigidbody set to "Is Kinematic". Any help would be appreciated!
//1.they don't collide or nothing happens when they collide & your script has now collision function !
void OnCollisionEnter(Collision col)
{
if(col.tag == "wall")//tag the objects in unity & test it
{
//do some thing
}
}
Answer by pixxelbob · Mar 02, 2016 at 11:23 PM
You need to actually implement the collision detection. You do this by having the OnCollisionEnter callback in your script or to separate behaviour create a new separate script on your car eg. CarCollision.
For example, you could also add an AudioSource component to your car, set a crash sound to the AudioSource, add the following script to your car & it will play the crash sound when the car collides with something.
using UnityEngine;
using System.Collections;
public class CarCollision : MonoBehaviour {
AudioSource audio;
void Awake() {
audio = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision collision) {
if (collision.relativeVelocity.magnitude > 2)
audio.Play();
}
}
Your answer
Follow this Question
Related Questions
why my object pass through 0 Answers
Special collision for players 1 Answer
Rigidbody randomly going through collider 1 Answer
How should I move my character? 0 Answers