- Home /
How can i add a collision detection for a character controller?
The problem is that my scripts work fine, except that the character walks trhough geometry even though it has a character controller attachhed to it. I have two scripts. One named TP_Motor, and the other TP_Controller. Here are the scripts: TP_Motor
using UnityEngine; using System.Collections;
public class TP_Motor : MonoBehaviour { public static TP_Motor Instance;
public float movementSpeed = 10;
public float backwardSpeed = 5;
public float Gravity = 40f;
public float TerminalVelocity = 20f;
private Vector3 slideDirection;
public float turningSpeed = 60;
public float MoveSpeed = 10f;
public float JumpSpeed = 14f;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
void Update()
{
Instance = this;
}
public void UpdateMotor()
{
SnapAlignCharacterWithCamera();
Moveandrotate();
ProcessMotion();
}
void Moveandrotate() // Esto hace que el jugador se mueva y rote.
{
float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
transform.Rotate(0, horizontal, 0);
float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
transform.Translate(0, 0, vertical);
}
void ProcessMotion()
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed Esto hace que se mueva de derecha a izquierda
MoveVector *= MoveSpeed;
// Reapply VerticalVelocity MoveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity , MoveVector.z);
// ApplyGravity
ApplyGravity();
// Move Character in World Space Esto tambien hace que se mueva de derecha a izquierda
TP_Controller.CharacterController.Move(MoveVector * Time.deltaTime);
}
void ApplyGravity()
{
if (MoveVector.y > -TerminalVelocity)
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
if(TP_Controller.CharacterController.isGrounded && MoveVector.y < -1)
MoveVector = new Vector3(MoveVector.x, -1 , MoveVector.z);
}
public void Jump()
{
if (TP_Controller.CharacterController.isGrounded)
VerticalVelocity = JumpSpeed;
}
void SnapAlignCharacterWithCamera()
{
}
}
AND THE TP_Controller
using UnityEngine; using System.Collections;
public class TP_Controller : MonoBehaviour { public static CharacterController CharacterController; public static TP_Controller Instance; public float MoveSpeed = 10f;
void Awake()
{
CharacterController = GetComponent();
Instance = this;
}
void Update()
{
if(Camera.mainCamera == null)
return;
GetLocomotionInput();
HandleActionInput();
TP_Motor.Instance.UpdateMotor();
}
void GetLocomotionInput()
{
var deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y; // Gravedad
TP_Motor.Instance.MoveVector = Vector3.zero;
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
}
void HandleActionInput() // Esto hace junto con jump que se accione con un boton el Jump
{
if (Input.GetButton("Jump"))
{
Jump ();
}
}
void Jump()
{
TP_Motor.Instance.Jump ();
}
}
Answer by InfiniBuzz · Jun 09, 2013 at 10:38 PM
Hi
make sure your walls or whatever object the character should collide with have colliders (and rigidbodies where needed) too.
they do have colliders, the script has some problem, because if i use other scripts there is no problem at all.
sorry for posting this as an answer not as a comment. I'll have a closer look
Your answer
![](https://koobas.hobune.stream/wayback/20220613103847im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer
Character Controller meets Rigidbody 1 Answer
Problem with character controller and collisions 1 Answer
Collision CharacterController vs CharacterController 2 Answers
NPCs fall through hills in terrain 1 Answer