- Home /
My player gameobject is ignoring colliders...
Hey people! I'm working on a 2D sidescrolling game, and a major problem I found with the player is that it completely ignores colliders. This is the first game I've scripted by myself and my code is reaaally janky, so be prepared... Any help would be extremely appreciated! Thanks guys :D
//For changing tiles
var left: Texture;
var right: Texture;
var front: Texture;
var back: Texture;
var idle: Texture;
//For changing the movement
var moveSpeed:float = 1;
//Vars for changing the animation
var uvAnimationTileX = 3; //Here you can place the number of columns of your sheet.
//The above sheet has 24
var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.
//The above sheet has 1
var framesPerSecond = 10.0;
function Start ()
{
transform.position.y = 15;
}
function Update ()
{
if (Input.GetKey(KeyCode.D))
{
renderer.material.mainTexture = right;
//Changes character position and limits positions
transform.position.x = transform.position.x + moveSpeed;
// Calculate index
var index: int = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);
// Size of every tile
var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
// split into horizontal and vertical index
var uIndex = index % uvAnimationTileX;
var vIndex = index / uvAnimationTileX;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
if (Input.GetKey(KeyCode.A))
{
renderer.material.mainTexture = left;
transform.position.x = transform.position.x - moveSpeed;
var indexA: int = Time.time * framesPerSecond;
indexA = indexA % (uvAnimationTileX * uvAnimationTileY);
var sizeA = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
var uIndexA = indexA % uvAnimationTileX;
var vIndexA = indexA / uvAnimationTileX;
var offsetA = Vector2 (uIndexA * sizeA.x, 1.0 - sizeA.y - vIndexA * size.y);
renderer.material.SetTextureOffset ("_MainTex", offsetA);
renderer.material.SetTextureScale ("_MainTex", sizeA);
}
if (Input.GetKey(KeyCode.W))
{
renderer.material.mainTexture = back;
transform.position.z = transform.position.z + moveSpeed;
if (transform.position.z >= 22.0) {
transform.position.z = transform.position.z - 1.0;
}
var indexW: int = Time.time * framesPerSecond;
indexW = indexW % (uvAnimationTileX * uvAnimationTileY);
var sizeW = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
var uIndexW = indexW % uvAnimationTileX;
var vIndexW = indexW / uvAnimationTileX;
var offsetW = Vector2 (uIndexW * sizeW.x, 1.0 - sizeW.y - vIndexW * sizeW.y);
renderer.material.SetTextureOffset ("_MainTex", offsetW);
renderer.material.SetTextureScale ("_MainTex", sizeW);
}
if (Input.GetKey(KeyCode.S))
{
renderer.material.mainTexture = front;
transform.position.z = transform.position.z - moveSpeed;
if (transform.position.z <= -7.0) {
transform.position.z = transform.position.z + 1.0;
}
var indexS: int = Time.time * framesPerSecond;
indexS = indexS % (uvAnimationTileX * uvAnimationTileY);
var sizeS = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
var uIndexS = indexS % uvAnimationTileX;
var vIndexS = indexS / uvAnimationTileX;
var offsetS = Vector2 (uIndexS * sizeS.x, 1.0 - sizeS.y - vIndexS * sizeS.y);
renderer.material.SetTextureOffset ("_MainTex", offsetS);
renderer.material.SetTextureScale ("_MainTex", sizeS);
}
}
Answer by TheDarkVoid · May 16, 2013 at 08:33 PM
It's moving though colliders because your not using a CharacterController or a Rigidbody. I would recommend a CharacterController since it can be easily added to your current setup. Something like this, or if you want a more precise control and want to go through the extra steps you can do this.
Well I tried testing the more precise character control and it cant move, and just falls through the box colliders underneath it >_>
Your answer