- Home /
detection raycast too late
so i've been trying to use a raycast to detect the collision of my character on ground. I have the isGrounded=Raycast inside fixedUpdate. The detection however, happens every frame and not every physics step?
code:
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour {
float direction = 1;
float speed = 0.01f;
float jumpHeight = 0.03f;
float fallSpeed = 0.1f;
bool isGrounded;
float velx = 0;
float vely = 0;
void Update () {
Move();
}
void FixedUpdate(){
isGrounded = Physics2D.Raycast (transform.position, -Vector2.up,0.13f,~(1<<8));
}
void Move(){
if (velx < -0.1f){
transform.eulerAngles = new Vector2(0,180);
}
if (velx > 0.1f){
transform.eulerAngles = new Vector2(0,0);
}
velx = direction * speed;
if (isGrounded) {
vely = 0;
if (Input.GetButtonDown("Jump")) {
vely = jumpHeight;
}
} else {
vely -= fallSpeed * Time.deltaTime;
}
transform.Translate (new Vector2(velx,vely));
}
}
any ideas?
Answer by robertbu · Feb 28, 2014 at 05:32 PM
'transform.Translate()' is not a physics step. You are directly manipulating the transform (teleporting the object to a new location). In addition, Update() can be called more frequently than FixedUpdate(). Moving your Raycast() into Update() should fix the immediate problem.
Thank you for the response, but your solution did not work.. I understand that transform.Translate isn't a physics step, but it needs to happen more than every frame, because otherwise it will go through objects. Or it needs to calculate how far it can travel on the last frame before hitting something.
Here's a screenshot of the problem
Answer by DemonJAZ · Jun 30, 2019 at 05:26 AM
Hi i am facing same issue can you explain how you solved it?