OnCollisionEnter Trigger
I am creating an OnCollisionEnter trigger, but having syntax trouble.
if I want OnCollisionEnter to stop transform.translate, how do I write that?
OnCollisionEnter; transform.translate(0,0,0)
I read the documentation, which wrote OnCollisionEnter as a void, however I get an error regarding the 'void' part.
I didn't think about that.
...And don't call me Shirley.
for the "void" part: make sure you use C# for the stop "transform.translate": we need a bit more information here, if you translate you objekt in the update method a simple "transform.translate(0,0,0)" in the OnCollisionEnter/OnTriggerEnter will not work
Answer by Cepheid · Apr 21, 2016 at 12:55 PM
Hi there @TheRedGuy90
It's pretty plain to me here that you're probably misunderstanding what OnCollisionEnter is. OnCollisionEnter is a method and thus cannot be used as a statement. To use the OnCollisionEnter method you must define the method within your class and then perform the statements within it, for example:
void OnCollisionEnter (Collision other)
{
transform.Translate(0,0,0);
}
That will now check for a collision and then stop the player's movement when they collide with an object. If this does indeed sound like the problem you were having I would highly recommend you pick up a beginner's book on C# before attempting to go any further otherwise it's going to be an uphill struggle.
I hope this helps!
Parsing error: $$anonymous$$ember names cannot be the same as their enclosing type
Could please post the entire class that is causing this problem? The error you have shown is occurring because you have named the method the same name as the class. Please ensure that your class name is seperate from the OnCollisionEnter() method. For example:
using UnityEngine;
using System.Collections;
public class Exampleclass : $$anonymous$$onoBehaviour
{
void OnCollisionEnter (Collision other)
{
transform.Translate (0, 0, 0);
}
}
I changed the name of the class, which fixed it.
But the script does nothing so far. I would like my character (which the script is attached to) to recognize this when it runs into any collider. I'd like to get rid of Unitys physics, I'm having too many problems with it, so I am writing my own.