- Home /
'height' is not a member of 'UnityEngine.Collider'.
I'm desperately fighting for some answers here.
ignoring the stuff thats been disabled, what am I doing wrong here? And why doesn't unity recognise height? Information taken from this tutorial: http://www.packtpub.com/article/unity-3.x-rigidbody-character-controller-script
the "height" problem is found under "fixed update". Please hlep, thanks. I have no idea what's happening.
#pragma strict
var health : int = 100;
//var speed : int = 5;
//var jumpSpeed: float = 8;
//var isgrounded : boolean = true;
public var jumpSpeed : float = 500.0;
public var jumpDirection : Vector3 = Vector3.zero;
public var isGrounded : boolean = false;
public var Jumping : boolean = false;
public var inAir : boolean = false;
public var airControl : float = 0.5;
public var MoveDirection : Vector3 = Vector3.zero;
public var Speed : float = 5.0;
function Start () {
}
function Update () {
// if(isgrounded == true){
// if (Input.GetKeyDown (KeyCode.Space)){
// rigidbody.velocity += Vector3.up * jumpSpeed;
// Physics.gravity = Vector3(0,-50,0);
// }
//}
// if(Input.GetAxis("Horizontal")){
// transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
// }
var AT = gameObject.GetComponent(AnimateTexture); //Store AnimateTexture Script
if(Input.GetKey("a")){ //Player moves left
AT.rowNumber = 1; //Change to running animation
} else if(Input.GetKey("d")){ //Player moves right
AT.rowNumber = 1; //Change to running animation
} else { //Player is not moving
AT.rowNumber = 0; //Change to idle animation!
}
if(Input.GetKey("space")){
AT.rowNumber = 3; //Player jumping animation
}
if(Input.GetKey("up")){
AT.rowNumber = 3; //Player jumping animation
}
if(Input.GetKey("s")){
AT.rowNumber = 4; //Player squats down
}
if(Input.GetKey("left")){
AT.rowNumber = 1;
}
if(Input.GetKey("right")){
AT.rowNumber = 1;
}
if(Input.GetKey("down")){
AT.rowNumber = 4;
}
}
// function OnCollisionEnter(theCollision : Collision){
//if(theCollision.gameObject.name == "floor"){
// isgrounded = true;
// }
//}
// function OnCollisionExit(theCollision : Collision){
// if(theCollision.gameObject.name == "floor"){
// isgrounded = false;
// }
//}
function FixedUpdate (){
if (!isGrounded){
if (Physics.Raycast(transform.position, -transform.up, collider.height/2 + 0.2)){
isGrounded = true;
Jumping = false;
inAir = false;
}
else if (!inAir){
inAir = true;
jumpDirection = MoveDirection;
}
}
Movement();
}
function OnCollisionExit(collisionInfo : Collision){
isGrounded = false;
}
function Movement(){
if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical")) {
MoveDirection = Vector3(Input.GetAxisRaw("Horizontal"),
MoveDirection.y,Input.GetAxisRaw("Vertical"));
}
if (Input.GetKeyDown("space") && isGrounded){
if (Input.GetButtonDown("Jump") &&isGrounded){
Jumping = true;
jumpDirection = MoveDirection;
rigidbody.AddForce((transform.up) * jumpSpeed);
}
if (isGrounded)
this.transform.Translate((MoveDirection.normalized * Speed) *
Time.deltaTime);
else if (Jumping || inAir)
this.transform.Translate((jumpDirection * Speed * airControl) *
Time.deltaTime);
}
}
Answer by rutter · Oct 25, 2013 at 09:54 PM
The tutorial was probably relying on dynamic typing. It's a powerful language feature, but it's not supported on mobile and is disabled when you're using #pragma strict
.
(Side note: I'm of the opinion that you should always use strict. You can code faster without it, sometimes, but it's really easy to miss problems like this one.)
Some colliders, such as CapsuleCollider, do have a height attribute. The reference returned by collider
doesn't know which type of collider is attached, though, so it returns the most general type it can: Collider.
If you know for sure that it's a capsule collider, you could cast the reference:
((CapsuleCollider)collider).height
As an alternative, every collider also has a bounds attribute you could use:
collider.bounds.size.y
I'm using a regular box collider, because I'm making a 2d game. During platfor$$anonymous$$g parts, the character can gradually slope off because of the capsule collider, so I switched from a character controller to a rigid body, and now i'm having an infinite number of problems lol.
Anyways, it's a basic cube with a 32bit alpha texture on it, and the collider is a box. What do I do to fix the code?
I tried:
if (Physics.Raycast(transform.position, -transform.up, ((BoxCollider)collider).height/2 + 0.2)){
but that didn't work. I also tried:
if (Physics.Raycast(transform.position, -transform.up, collider.bounds.size.y.height/2 + 0.2)){
but then I get the error "'height' is not a member of 'float'." :(
This is the answer you want. I'd recommend type casting over using bounds, but whatever works.
what is an example code of what you recommend? Please take into account my response above yours. Thanks for the help guys..
The character is also not moving, with this code/tutorial... sigh.. lol...
Your answer
Follow this Question
Related Questions
Wall jump, jump movement not working 0 Answers
How to limit only the Rigidbody.velocity from player input? 1 Answer
Rigidbody NavMeshAgent Causing Crazy Collisions with Player. 0 Answers
Problem with Rigidbody control script! (collision with perpendicular walls problem) 1 Answer
2.5D Shooting Rigidbodies vs RayCasts 0 Answers