Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by mnm2317 · Apr 16, 2012 at 10:01 PM · c#raycastcolliders

How to test colliders C#

I have a rotating ball on a plane that the player rotates. I need to test/check to see that the ball is still on the plane (that way, if not, I can stop the game).
I've tried using raycast with no success, and that seems to be because both objects have the ability to rotate. Here's what I've used:

         Vector3 Up = transform.TransformDirection(Vector3.up);
         if (Physics.Raycast(transform.position, Up, 10))
             print("There is something in front of the object!");


It seems I need to not only check if the two objects are touching, but I need to check a little radius around the ball since it can end up slightly off the plane if you tilt the plane to it's extremes. I thought raycast would be perfect here, but no avail. Thanks for the help.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DJSwiti · Apr 16, 2012 at 10:13 PM

 public void OnCollisionStay(Collider col)
     {
         if (col.gameObject.tag == "wall")
             Debug.Log("There is something in front of the object!");
     }

with that script attached to the plane and the sphere tagged as a wall or vice versa.

Comment
Add comment · Show 6 · 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
avatar image mnm2317 · Apr 17, 2012 at 07:46 PM 0
Share

It's saying "OnCollisionStay" is a script error. Is it C#?

avatar image Lo0NuhtiK · Apr 17, 2012 at 07:55 PM 0
Share

OnCollisionStay(Collision col)

avatar image mnm2317 · Apr 19, 2012 at 06:55 PM 0
Share

Ok, "OnCollisionStay" gives me no errors (looks like your original answer said "Collider col" when it needed "Collision col", at least according to the link there.
Problem is I put that code in the 'plane' script (replaced "wall" with "ball") and I get no errors but I also get no debug message either.

avatar image DJSwiti · Apr 20, 2012 at 05:01 PM 0
Share

Yea sorry it was Collision and not Collider.. But it means that your objects aren't in collision.. Have you tried to move the cube while in collision with the plane ? Do you still have no Debug.Log ?

avatar image fafase · Apr 20, 2012 at 05:05 PM 0
Share

did you tag the object or name the object? $$anonymous$$ake sure you use the proper one.

Show more comments
avatar image
0

Answer by Dead Lincs · Feb 03, 2014 at 02:19 PM

I did it like this: (By the way my character controller etc are all written in C#. I can give you the package if you would like, just send me a message

 using UnityEngine;
 using System.Collections;
 
 public class CrouchC : MonoBehaviour {
     public float walkSpeed = 7.0F;
     public float crouchSpeed = 3.0F;
     public float runSpeed = 20.0F;
 
     private CharacterMotorC chMotor;
     private CharacterController ch;
     private Transform tr;
     private float dist;
     private bool release = false;
     private float vScale = 1.0F;
     private float speed;
 
     void Start(){
         chMotor = GetComponent<CharacterMotorC>();
         tr = transform;
         ch = GetComponent <CharacterController>();
         dist = ch.height / 2;
         speed = walkSpeed;
     }
 
     void Update(){
         InputCheckingRule ();
         ApplyChanges ();
     }
 
     public void InputCheckingRule(){
         if (release == false) {
             vScale = 1.0F;
             speed = walkSpeed;
         }
         if (chMotor.grounded && Input.GetKey ("left shift") || Input.GetKey ("right shift")) {
             speed = runSpeed;
         }
         if (Input.GetKey ("right shift")) {
             vScale = 0.35F;
             speed = crouchSpeed;
             release = true;
         }
         if (Input.GetKeyUp ("right shift")) {
             release = true;
         }
         if (release == true) {
             ReleaseCheckingRule();
                 }
     }
 
     public void ReleaseCheckingRule(){
         Vector3 Up = transform.TransformDirection(Vector3.up);
         if (Physics.Raycast (transform.position, Up, 10)) {
                         release = true;
                 } else {
             release = false;
                 }
     }
 
     public void ApplyChanges(){
         chMotor.movement.maxForwardSpeed = speed;
         float ultScale = tr.localScale.y;
         Vector3 temp = tr.localScale;
         temp.y = Mathf.Lerp (tr.localScale.y, vScale, 5 * Time.deltaTime);
         tr.localScale = temp;
         Vector3 temp2 = tr.position;
         temp2.y += dist * (tr.localScale.y - ultScale);;
         tr.position = temp2;
     }
     
 }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use a sphere collider to tell if my player is on the ground? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag, and which should I use when? 1 Answer

GUI, SpellBar and C#-related questions 1 Answer


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