- Home /
Question by
Ochreous · Jun 18, 2013 at 05:49 PM ·
c#rigidbodycharactercontroller
C# Need Help converting from Rigidbody to Character Controller
Hi everyone, I'm trying to convert the DontGoThroughThings script on the unify wiki so it will work with Character Controllers instead of rigidbodies. I'm getting this error from the console say charactercontroller doesn't exist in current context. How do you set it so that myCharacterController equals the Character Controller that is attached to the gameobject? Also if you see anything else wrong with my script please let me know.
using UnityEngine;
using System.Collections;
public class DontGoThroughThings : MonoBehaviour
{
public LayerMask layerMask; //make sure we aren't in this layer
public float skinWidth = 0.1f; //probably doesn't need to be changed
private float minimumExtent;
private float partialExtent;
private float sqrMinimumExtent;
private Vector3 previousPosition;
private CharacterController myCharacterController;
//initialize values
void Awake()
{
myCharacterController = charactercontroller;
previousPosition = myCharacterController.transform.position;
minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
partialExtent = minimumExtent * (1.0f - skinWidth);
sqrMinimumExtent = minimumExtent * minimumExtent;
}
void FixedUpdate()
{
//have we moved more than our minimum extent?
Vector3 movementThisStep = myCharacterController.transform.position - previousPosition;
float movementSqrMagnitude = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent)
{
float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
RaycastHit hitInfo;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))
myCharacterController.transform.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;
}
previousPosition = myCharacterController.transform.position;
}
}
Comment
Your answer
Follow this Question
Related Questions
C# FP_Camera not following Character Controller 0 Answers
C# DragRigidbody ArgumentException: get_main can only be called from the main thread 2 Answers
C# CharacterController.SimpleMove Goes on Forever 1 Answer
C# Character Controller Collision If Statement 2 Answers
C# RotateAround for 2DRigidBodies 1 Answer