- Home /
raycast and isgrounded dont work
i created a control script for my character that uses character controller script(from standards assets). i have a problem:
var Speed2 = 15;
var Speed3 = 5;
var Rota = 50;
var jumpspeed:float;
var anim:Animation;
var gravity=10.0;
var jumpstep:int=0;
private var moveDirection : Vector3 = Vector3.zero;
anim = GetComponent(Animation);
var forward : Vector3;
var up : Vector3;
var poshelper:Vector3;
var poshelper2:Vector3;
var charp:GameObject;
var pushPower = 2.0;
var speedaxis:float;
var diraxis:float;
var grounded:boolean;
var congrounded:boolean;
public var steps:int=30;
function Awake () {
charp=GameObject.Find("charp");
anim.wrapMode = WrapMode.Loop;
}
function Update ()
{
var up = transform.TransformDirection(Vector3.up);
var hit : RaycastHit;
Debug.DrawRay(charp.transform.position, -up * 0.5, Color.green);
poshelper2=charp.transform.position;
forward= transform.TransformDirection(Vector3.forward);
up= transform.TransformDirection(Vector3.up);
var controller : CharacterController = GetComponent(CharacterController);
controller.Move(-(new Vector3(0,1,0)*gravity*Time.deltaTime));
for (var state : AnimationState in animation) {
state.speed=3.0;
if(Input.GetKeyDown("space")){
if(controller.isGrounded){
jumpstep=steps;
}
}
grounded=Physics.Raycast(charp.transform.position, -up, hit, 0.5);
congrounded=controller.isGrounded;
diraxis=Input.GetAxis("Horizontal");
speedaxis=Input.GetAxis("Vertical");
controller.Move(transform.TransformDirection(Vector3.right)*Speed3*diraxis*Time.deltaTime);
controller.Move(transform.TransformDirection(Vector3.forward)*Speed3*speedaxis*Time.deltaTime);
if(speedaxis!=0&&Physics.Raycast(charp.transform.position, -up, hit, 0.5)){
anim.CrossFade("walk");
}else{
state.speed=0.5;
anim.CrossFade("idle");
}
if(jumpstep>0){
if(jumpstep>10){
controller.Move(up*jumpspeed*Time.deltaTime);
jumpstep--;
}else{
controller.Move(up*jumpspeed*0.5*Time.deltaTime);
jumpstep--;
}
}
}
poshelper=charp.transform.position;
}
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic) { return; }
if (hit.moveDirection.y < -0.3) { return; }
var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}
the problem is that i cant figure out when im falling or on the ground, becuase controller.isgrounded dont work, i also tried with raycasts but it also dont work, i tried to debug it by adding two varriables called grounded and congrounded as u can see in the code they are boolean and change in update, but when my character is on the ground non work, the ground var is always off and the raycast thing only trigger randomly when i jump, can you figure out whats going on? thanks.
did you forget to have a collider somewhere? LOO$$anonymous$$ at all the colliders in your scene.
Your answer
Follow this Question
Related Questions
Raycast2D Only returns true 1 Answer
Send Raycast to ground, place player at Raycast hit? 1 Answer
ground check isnt working...HELP! 2 Answers
Falling then Apply Damage. 0 Answers
XRRayInteractor Offset Ray Direction 0 Answers