Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TeraTech · Feb 05, 2016 at 11:21 PM · collisionjumpjumping objectlaunchingcatapult

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LazyElephant · Feb 06, 2016 at 12:35 AM 1
Share

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.

avatar image TeraTech LazyElephant · Feb 06, 2016 at 04:18 AM 0
Share

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!!

avatar image TeraTech LazyElephant · Feb 06, 2016 at 10:04 PM 0
Share

@LazyElephant

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);

 }
avatar image LazyElephant TeraTech · Feb 07, 2016 at 01:46 PM 0
Share

Is it working for you now? Or are you still getting strange behavior?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

49 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't jump/buggy when standing next to an object. 0 Answers

Player intersecting with ground during jump landing 1 Answer

Endless Game : Ball collision issue when it moves from one cube(ground) to another cube (ground),Ball collision problem 0 Answers

Copy collision object position (Or just "fly" a little bit) 0 Answers

how to stop infinite jumping in air? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges