# My Collision code isnt working... im new so anyhelp would be apprieciated

so I’m making a game for the first time, and I have created a player using the character controller method rather than rigid body, and here is my problem, how should I be going about enemy collision?
here’s what I have done so far just so you might be able to help me.
character controller script:

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class playermovement : MonoBehaviour
{
public CharacterController controller;
public float walkSpeed =10f;
public float gravity = -9.81f;
public float jumpheight = 3f;
public float runSpeed = 20f;

public Transform groundCheck;
public float groundDistance = 0.4f;

Vector3 velocity;
bool isGrounded;

// Update is called once per frame
void Update()
{

if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}

float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);
float speed;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = runSpeed;
}
else
{
speed = walkSpeed;
}

Vector3 move = transform.right * x + transform.forward * z;

controller.Move(move * speed * Time.deltaTime);

if(Input.GetButtonDown(“Jump”) && isGrounded)
{
velocity.y += Mathf.Sqrt(jumpheight * -2f * gravity); }

velocity.y += gravity * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);

}

}

and here’s my code for collision:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collide : MonoBehaviour
{

void OcCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Enemy”)
{
GetComponent().enabled = false;
Debug.Log(“hit”);
}
}
}

also i did add a boxcollider to the player character, not sure if it was needed but i figured it would be neccesary in order to collide… like i said im brand new to c# let alone unity, so im probably being dumb lol

OcCollisionEnter?

Yeah, as Chubz points out, you’re just making typos. Don’t do that. See below.

But a bigger problem is that this script is derived from defective Unity sample code and will forever give you difficulty detecting the ground contact, potentially causing all kinds of malfunctions.

If you call .Move() twice in one single frame, the grounded check may fail. <<- you are doing this above

I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

Here is a work-around:

https://discussions.unity.com/t/811250/2

I recommend you also go to that same documentation page and ALSO report that the code is broken.

When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

https://discussions.unity.com/t/855344

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!

The above code MAY not be affected because of how you’re doing the ground check, but future modifications could trip you up, plus it’s just piggish to call .Move() more than once per frame.

Finally, keep this in mind to save yourself a lot of time:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Finally, when you have errors, don’t post here… just go fix your errors! Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

• the description of the error itself (google this; you are NEVER the first one!)
• the file it occurred in (critical!)
• the line number and character position (the two numbers in parentheses)
• also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

future reference, it’s always good to have class names start with a capital letter, as most calls to any sort of component do. And lowercase letters are used for references, so you know they are “after the fact”, basically. As you dive deeper into coding this will confuse you:

``````playermovement playermovements;
``````

if not following rule of thumb, as an common example would look more like this:

``````PlayerMovement playerMovement;
``````

A lot more things will confuse you anyway, as time goes on, so best to keep things clear and as far away from typos as humanly possible.