Player flies off after jumping after collision.
I'm learning Unity3d (using 5.3 pro) and have been doing ok with it. However, I have one glitch in my self-taught demo that I can't figure out.
When the player jumps, everything works fine. It is using a force of 345f and an animation. If I jump in the direction of a 1x1m platform, I want the player to either clear the jump, or be prevented from continuing to move forward if he collides with it.
That does happen most of the time, but occasionally, the player will get stuck in the geometry of the obstacle and if you jump just right, the player will fly off as if launched by a catapult. It is pretty fun to see how far I can make it, but that is not how it should be working. Control code below:
using UnityEngine;
using System.Collections;
public class PlayerControls : MonoBehaviour {
public float accel = .5f;
public float maxSpeed = .15F;
public float maxHeight = 7f;
public float turnSpeed = 4f;
public float jumpForce = 345f; //340f - 345fworks well with the animation
private Rigidbody _rb;
private float _distToGround;
private Animator _anim;
private bool _jumped = false;
// Cameras
public Camera povCam;
private Camera gameCam;
// Use this for initialization
void Start() {
Debug.Log("PlayerControls Initialized");
_anim = GetComponent<Animator>();
gameCam = Camera.main;
povCam.enabled = false;
_rb = GetComponent<Rigidbody>();
_distToGround = GetComponent<Collider>().bounds.extents.y;
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.V)) {
gameCam.enabled = !gameCam.enabled;
povCam.enabled = !povCam.enabled;
}
}
void FixedUpdate() {
isoControls();
}
void isoControls() {
//Debug.Log("Distance to ground: " + _distToGround);
float turn = Input.GetAxis("Horizontal") * turnSpeed;
float move = Mathf.Clamp(Input.GetAxis("Vertical") * maxSpeed, 0f, maxSpeed);
// Animation
_anim.SetFloat("vSpeed", Input.GetAxis("Vertical"));
// Jump
if (isGrounded()) {
if (Input.GetButton("Jump")) {
jump();
}
}
// Forward / Backward Movement
transform.position += transform.forward * move;
// Turning Left / Right
transform.Rotate(Vector3.up, turn);
}
void fpsControls() {
// TODO::
}
bool isGrounded() {
return Physics.Raycast(transform.position, -Vector3.up, _distToGround + .1F);
}
void jump() {
if (!_jumped) {
_jumped = true;
_anim.SetBool("jump", true);
Vector3 force = transform.up * jumpForce;
_rb.AddRelativeForce(force, ForceMode.Impulse);
}
_jumped = false;
}
}
I am using a Capsule Collider. I am using a RigidBody with the following settings: Mass: 90 Drag: 0 Angular Drag: .05 Use Gravity: true Is Kinematic: false Interpolate: None (also tried the others) Collision Detection: Discrete (also tried the others)
The camera is isometric, and so are the controls. I don't want to use any pre-built controls because I'm trying to learn and not copy/paste.
Any help is appreciated!
The weird behavior you're seeing is probably a result of setting the object position manually at lines 61-64 while also using rigidbody forces to jump. You shouldn't set the transform position directly if you're going to use a non-kinematic rigidbody. You should use forces/velocity ins$$anonymous$$d.
Ah that actually makes a lot of sense. I would never dream of using position in my Flash development (back in the day). Don't know why I thought it was ok to do that here. I'll try making changes and update this page. Thanks!!
Thank you again for your observation. I fixed the code to use velocity. I also fixed it so that my player can't control himself while in the air. Below is my new solution:
void isoControls() {
float turn = Input.GetAxis("Horizontal") * turnSpeed;
float move = $$anonymous$$athf.Clamp(Input.GetAxis("Vertical") * maxSpeed, 0f, maxSpeed);
// Animation
_anim.SetFloat("vSpeed", Input.GetAxis("Vertical"));
if (isGrounded()) {
// $$anonymous$$ovement
_rb.velocity = new Vector3(transform.forward.x * move, 0.0f, transform.forward.z * move);
// Jump
if (Input.GetButton("Jump")) {
jump();
}
}
// Turning Left / Right
transform.Rotate(Vector3.up, turn);
}
Is it working for you now? Or are you still getting strange behavior?
Your answer