Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ntbsweeps · Mar 01, 2016 at 08:43 AM · collisionrigidbodycollidercharactercontroller

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ali-hatem · Mar 01, 2016 at 10:21 AM 0
Share
 //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
    }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
     
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

why my object pass through 0 Answers

Special collision for players 1 Answer

Rigidbody randomly going through collider 1 Answer

OnTriggerEnter doesn't work if the colliding object is not moving. Translating it by (0,0,0) fixes it. What's the problem? 4 Answers

How should I move my character? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges