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 WeirdDeveloping · Apr 11, 2016 at 08:09 AM · not workingcolliding

Voids/functions failing to be called

I am making a ball move when it collides with the player and pressed the key i, however i have done debug.log tests with the scripts and it appears that the OnCollisionEnter is not working. Here is the script

 using UnityEngine;
 using System.Collections;
 
 public class KickForce : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         print ("This is printed");
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnCollisionEnter (Collision collision) {
 
         if(collision.gameObject.tag == "Player") 
         {
             print ("this has not worked");
 
             if (Input.GetKey ("i")) {
                 print("this has not appeared on the console");
                 transform.Translate(Vector3.right * Time.deltaTime * 10);
             }
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fredex8 · Apr 12, 2016 at 01:32 AM

Assuming your objects have colliders and rigidbodies and collision genuinely is tagged as Player then "this has not worked" will print. There is nothing wrong with that part of the code.

However the code below your input is not going to work. OnCollisionEnter is only called once, on the frame at which they collided.

Your input if (Input.GetKey ("i")) { is therefore only going to be called once at most too, only really if the player was holding down the key when the collision happened. The chances of the player being able to hit i at the exact same frame that the collision occurred are pretty much zero.

This means that transform.Translate(Vector3.right * Time.deltaTime * 10); is only possibly going to happen once too. Time.deltatime is also going to dramatically scale down the value of the translate. At 60 frames per second you are going to be moving the ball right by 0.16... which probably isn't even going to be noticeable.

You should set a boolean on collision rather than trying to put the input directly below it and use something like AddForce to kick the ball as just arbitrarily telling the ball to move right is going to look very rigid and unnatural. I would also put this script on the player, not the ball.

Here's a quick example for you:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript: MonoBehaviour
 {
     private bool ballCheck;
     private Rigidbody ball;
 
     private float kickPower = 5;
     private Vector3 direction;
 
     // Use this for initialization
     void Start()
     {
         //This will need to be set to the direction you want to kick the ball in
         direction = transform.forward;
     }

     //Make sure your ball object has a rigidbody, collider and is tagged "Ball"

     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Ball")
         {
             ballCheck = true;
             ball = collision.gameObject.GetComponent<Rigidbody>();
         }
     }
 
     void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.tag == "Ball")
         {
             ballCheck = false;
         }
     }    
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.I))
         {
             if (ballCheck == true)
             {                
                 ball.AddForce(direction * kickPower);
             }
         }
     }
 }

...and for future reference if I had noticed that you had asked this question twice in one day without making any changes or improvements yourself I would not have bothered answering it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

gameobject doesn't spawn in the assigned random positions? 2 Answers

Unity3D not Opening Help!! 1 Answer

Unity not working in Opera 3 Answers

Buttons Don't always work. 2 Answers

why isn't my maps changes updated after i build the game and then tests it? 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