- Home /
How to get my character to collide with objects
Hi, I've been working on a small project for a few days and finally got a character controller working. I'm now working on it colliding with an object, but it keeps going through the object. I've tried using a rigid body and a character controller from the physics components but nothing really works as expected. I only had luck with the rigid body but it would push around the object.
Preferably I would like to get the character controller working. Here is my code so far for my charactercontroller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MechController : MonoBehaviour {
static Animator anim;
public float Speed = 20.0F;
public float rotationSpeed = 100.0F;
CharacterController controller;
float currentSpeed;
Rigidbody rbody;
void Start()
{
anim = GetComponent<Animator>();
Debug.Log (anim.name);
rbody = GetComponent<Rigidbody> ();
controller = GetComponent<CharacterController> ();
}
void Update() {
float translation = Input.GetAxis ("Vertical") * Speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
if(Input.GetButtonDown("Jump"))
{
anim.SetTrigger("isJumping");
}
if(translation != 0)
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool ("isRunning", false);
}
Vector3 velocity = transform.forward * currentSpeed;
controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2 (controller.velocity.x, controller.velocity.z).magnitude;
}
}
Any tips or tricks would be awesome. Thanks for your time! -Bobby
Answer by tormentoarmagedoom · Oct 09, 2017 at 08:14 AM
Uee RigidBody and Box/Capsule Coliiders on both objects. Check the "is Kinematic" option in rigidbodies.
Bye!
Having one rigidbody on one object only + colliders on each object is enough to detect collision
Checking
is$$anonymous$$inematic
will prevent the collisions from occuring as stated in the documentationIf is$$anonymous$$inematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140519im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Keeping characters within Fighting area 1 Answer
Gun collision problem 3 Answers
Character Physics 0 Answers
Physics for Moving Objects 0 Answers
Character Controller Slope 1 Answer