GameObject doesn't move
Hey guys,
I have a 2D game about landing with a spaceship. My spaceship has rigidbody, but I disabled gravity and I'm moving the ship towards the surface using transform.Translate. But the problem is, when I land on the ground, I want to move the player to the left. But nothing seems to work and I really don't have any idea why..
Any help would be appreciated :]
void Update () {
player.transform.Translate(0,-gravityForce + speed,0);
//my simulaion of gravity
if(Input.GetKey(KeyCode.Space)){
fuel -= 1f;
speed += 0.001f;
Instantiate(smoke, smokeEnd.transform.position, smokeEnd.transform.rotation);
} else {
speed -= 0.001f;
}
if ((player.transform.position.y < -2f)){ //landed on ground
if((speed <= upBoundary)&&(speed >= downBoundary)){
speed = 0;
gravityForce = 0;
onGround = true;
} else {
Crash();
onGround = true;
}
}
}
void FixedUpdate(){
if (onGround){
//debug here works fine
player.transform.Translate(new Vector3(-2f,0,0));
}
}
Does your first Translate Function work? and for the transform.translate at the bottom, multiply your Vector3 by Time.deltaTime
Yeah, the first one works just fine. Even after multiplying the bottom Vector3 it still doesn't work..
But this is my Crash() function, which I call and where I also move my player, could it somehow interfere?
void Crash(){
player.transform.position = new Vector3(0,-2,0);
player.GetComponent<SpriteRenderer>().sprite = crashedSprite;
oops.SetActive(true);
GUItimer++;
if(GUItimer >= 100){
oops.SetActive(false);
playAgain.SetActive(true);
}
}
Answer by Zoelovezle · Dec 23, 2015 at 12:23 AM
Make your body Kinematic for creating your own Physics.Since Kinematics will be true , no collision will affect the your player.Use RaycastHit2D for detecting specific platforms and moving over them.
Read the following documentation: http://docs.unity3d.com/Manual/class-Rigidbody2D.html
Watch the following video: http://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting?playlist=17120
Ok it works now, altough I am not really sure why. I had some issues with ray casting so I didn't add that, but it seems it works now bcs I replaced rigidbody with rigidbody2d with Is$$anonymous$$inematic selected.