- Home /
Player is floating away... Whaa...
So I opened a old project in Unity 4.6 and the project was made in 4.3. I just noticed my player started floating away. It just floats upwards and just keeps going. There is nothing I can do. I tried invert the gravity from -9.81 to 300 and now the player is not moving up or down. What is this?! There is nothing in the code that does this.
The whole player script. (2D Player).
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Movement{
public float speed = 5.0f;
public float jumpForce = 10.0f;
}
[System.Serializable]
public class AnimatorSettings{
public string movingValue = "Speed";
public string jumpingValue = "Grounded";
public string verticalSpeedValue = "vSpeed";
}
[System.Serializable]
public class SoundEffects{
public GameObject jumpSound;
public GameObject dieSound;
}
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerController : MonoBehaviour {
public Movement movement;
public AnimatorSettings animatorSettings;
public SoundEffects soundEffects;
bool facingRight = true;
Rigidbody2D rig;
Animator anim;
TrailRenderer trail;
public bool grounded = false;
public LayerMask groundLayer;
RaycastHit2D hit;
void Start(){
rig = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
trail = GetComponent<TrailRenderer>();
rig.fixedAngle = true;
trail.enabled = false;
}
void Update(){
if(grounded && Input.GetButtonDown ("Jump")){
anim.SetBool(animatorSettings.jumpingValue, false);
rig.AddForce(new Vector2(0, movement.jumpForce * 10));
Instantiate(soundEffects.jumpSound, Vector3.zero, Quaternion.identity);
}
if(Physics2D.Raycast(transform.position, -Vector2.up, 0.18f, groundLayer)){
grounded = true;
} else {
grounded = false;
}
}
void FixedUpdate(){
anim.SetBool(animatorSettings.jumpingValue, grounded);
anim.SetFloat (animatorSettings.verticalSpeedValue, rig.velocity.y);
float move = Input.GetAxis("Horizontal");
anim.SetFloat(animatorSettings.movingValue, Mathf.Abs(move));
rig.velocity = new Vector2(move * movement.speed, rig.velocity.y);
if(move > 0 && !facingRight){
Flip();
} else if(move < 0 && facingRight){
Flip ();
}
}
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Something is causing it :D Changing the gravity proves that, its now being pushed down with such force that your other forces are having a negligible effect upon it, which is why it does not move. You need to be more specific and post some examples of your code and say how you have your rigidbody set up(use a pic if you like) if you want us to be able to help you properly :)
@$$anonymous$$rSoad The thing is that it worked when I first made the project. So I don't really need to show you any code. But this is how my rigidbody is set up $$anonymous$$ass: 1 Linear drag: 0 Angular drag: 0.05 Gravity scale: 1 Fixed angel: Yes Is kinematic: No Interpolate: None Sleeping $$anonymous$$ode: Start Awake Collision Detection: Discrete
I also posted the code I used and I fixed a typo. It was 4.3 I used since I created a small 2D platformer.
If animation is not looping properly than it can cause small shift eachtime.
If you have your gravity too high, then yeah it won't move, set your gravity to something like 20 and see.
@Yash I don't think there is anything wrong with the animation. @Dark_Tiger If I set it to 20 the players floats away.
Answer by RealMTG · Oct 28, 2014 at 12:56 PM
I just needed to disable Root Motion on the Animator and it was fixed.
Your answer
Follow this Question
Related Questions
Enemy Model Faces wrong direction 1 Answer
Player floating on the ground 1 Answer
Make trigger be activated only by the player 2 Answers
Bomb not effecting anything. 1 Answer
How do I make a sphere "know" when the player is nearby? 1 Answer