- Home /
Collision and character controllers
I have a simple character with collision and a rigidbody and some static boxes without rigidbodies which makes up the level. The character however falls through the boxes. Now this might seem simple enough, I Just need to add a character controller right. The problem is that I want my character to be capable of flying and I don't really care about jumping or sliding on slopes. Is there any way for me to create terran collision like this? I assume I'd have to program physics by myself and that's maybe not such a good idea considering I'm not really a good programmer. So, what can I do to remedy this...?
Here's my movement script if anyone is interested
using UnityEngine;
using System.Collections;
/// <summary>
/// Movement.
///
/// This script controls the player movement.
/// </summary>
public class Movement : MonoBehaviour {
public int acceleration = 20;
private Vector3 velocityToBeApplied;
private Transform playerTransform;
private int stopSpeed = 3;
// Use this for initialization
void Start ()
{
if(networkView.isMine == true)
{
velocityToBeApplied = Vector3.zero;
playerTransform = transform;
}
else
{
enabled = false;
}
}
// Update is called once per frame
void Update ()
{
if(Input.GetButton("MoveLeft"))
{
velocityToBeApplied = new Vector3((velocityToBeApplied.x - acceleration) * Time.deltaTime,
velocityToBeApplied.y, velocityToBeApplied.z);
}
if(Input.GetButton("MoveRight"))
{
velocityToBeApplied = new Vector3((velocityToBeApplied.x + acceleration) * Time.deltaTime,
velocityToBeApplied.y, velocityToBeApplied.z);
}
if(Input.GetButton("MoveForward"))
{
velocityToBeApplied = new Vector3(velocityToBeApplied.x, velocityToBeApplied.y,
(velocityToBeApplied.z + acceleration) * Time.deltaTime);
}
if(Input.GetButton("MoveBackwards"))
{
velocityToBeApplied = new Vector3(velocityToBeApplied.x, velocityToBeApplied.y,
(velocityToBeApplied.z - acceleration) * Time.deltaTime);
}
if(Input.GetButton("MoveUp"))
{
velocityToBeApplied = new Vector3(velocityToBeApplied.x, (velocityToBeApplied.z + acceleration) * Time.deltaTime,
velocityToBeApplied.z);
}
if(Input.GetButton("MoveDown"))
{
velocityToBeApplied = new Vector3(velocityToBeApplied.x, (velocityToBeApplied.z - acceleration) * Time.deltaTime,
velocityToBeApplied.z);
}
playerTransform.Translate(velocityToBeApplied);
velocityToBeApplied = Vector3.Lerp(velocityToBeApplied, Vector3.zero, Time.deltaTime * stopSpeed);
}
}
Yeah they do, static ones. No rigidbodies. I've got it working somewhat now... I made the players kinematic and had made a function zeroing rigidbody velocity and angular velocityever 0.1 seconds. However this solution is incredibly ugly and gives me some other problems. So yeah I'm doing it all wrong but I don't know what to replace it with.
Your answer
Follow this Question
Related Questions
No slipping off edges with Character Controller 0 Answers
Make Character Controller a Box? 2 Answers
My Character Controller Is Warping Randomly (/w video) 0 Answers
No Collision with character controller 1 Answer
Collision With Character Controller 4 Answers