Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Sep 15, 2020 at 04:42 AM by Amucity for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Amucity · Sep 14, 2020 at 02:55 PM · physicsrigidbodycollidermovement script

Player object can, but should not, climb walls, slingshot themselves, and given enough time, can run through the ground.

So I apologize in advance, as I am relatively new to several of these concepts, and I feel like a moron already. Additionally, I have searched the forums and google and I have yet to find an answer to this problem, but more than likely I just do not know how to properly convey what is occurring in such a manner as to have search engines return usable results.


I have a player game object with a capsule collider. If I position the player object (capsule) and rotate the camera to be looking upward at another game object with a collider, we'll use a box collider on a wall, but it happens on mesh colliders as well, it is allowing the player object to climb up the wall.


In addition, if I happen to run into that box collider, and maintain a forward heading, then turn slightly to slide off the collider, once I am clear of the collision, I get a boost of forward and upward velocity....Lastly, if I angle the camera downward, the player object will begin to work itself through the ground object.


Now I have tried continuous detection of collision on the Rigidbody, as well as increasing Mass and Drag values in order to minimize the upward and forward velocity. While the latter options minimize the issue of launching the player object, I would like to truly solve the issue, and if possible understand where I messed up or what I missed to cause this.


Thank you so much for your attention and assistance if you have a moment to spare it.


TL/DR: I messed up or missed something on Rigidbody/Collider/Code causing player object to inadvertently climb walls, tunnel through ground colliders, and build up momentum and rocket themselves when sliding off colliders.


Code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour { public GameObject cam; public Animator anim; public float speed; public float mspeed;

 Rigidbody rb;
 CapsuleCollider capsule;

 Quaternion cameraRot;
 Quaternion characterRot;
 bool cursorLocked = true;
 bool lockCursor = true;

 float x;
 float z;

 // Start is called before the first frame update
 void Start()
 {
     rb = this.GetComponent<Rigidbody>();
     capsule = this.GetComponent<CapsuleCollider>();
     cameraRot = cam.transform.localRotation;
     characterRot = this.transform.localRotation;
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
         rb.AddForce(0, 375, 0);
 }

 void FixedUpdate()
 {

     float yRot = Input.GetAxis("Mouse X") * mspeed;
     float xRot = Input.GetAxis("Mouse Y") * mspeed;

     cameraRot *= Quaternion.Euler(-xRot, 0, 0);
     characterRot *= Quaternion.Euler(0, yRot, 0);

     this.transform.localRotation = characterRot;
     cam.transform.localRotation = cameraRot;

     cameraRot = ClampRotationAxis(cameraRot);

     x = Input.GetAxis("Horizontal") * speed;
     z = Input.GetAxis("Vertical") * speed;

     transform.position += cam.transform.forward * z + cam.transform.right * x;  //new Vector3(x * speed, 0, z * speed);

     Quaternion ClampRotationAxis(Quaternion q)
     {
         q.x /= q.w;
         q.y /= q.w;
         q.z /= q.w;
         q.w = 1.0f;

         float anglex = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
         anglex = Mathf.Clamp(anglex, -30f, 15f);
         q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * anglex);

         return q;
     }

     UpdateCursorLock();


 }

 bool IsGrounded()
 {
     RaycastHit hitInfo;
     if (Physics.SphereCast(transform.position, capsule.radius, Vector3.down, out hitInfo,
             (capsule.height / 2f) - capsule.radius + 0.1f))
     {
         return true;
     }
     return false;
 }

 public void SetCursorLock(bool value)
 {
     lockCursor = value;
     if (!lockCursor)
     {
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
     }
 }

 public void UpdateCursorLock()
 {
     if (lockCursor)
         InternalLockUpdate();
 }

 public void InternalLockUpdate()
 {
     if (Input.GetKeyUp(KeyCode.Escape))
         cursorLocked = false;
     else if (Input.GetMouseButtonUp(0))
         cursorLocked = true;

     if (cursorLocked)
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
     else if (!cursorLocked)
     {
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
     }
 }

}

Settings: alt text

rigidbody-collider-script.jpg (86.4 kB)
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by Amucity · Sep 15, 2020 at 04:41 AM

Problem resolved.
The problem line of code was:

 transform.position += cam.transform.forward * z + cam.transform.right * x;  


should have been adjusted to:

 transform.position += this.transform.forward * z + cam.transform.right * x;

Reason being, the forward motion was being applied to camera direction, vs. the direction of the GameObject (which the camera was a child of)

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

349 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to fix this problem? 1 Answer

Cube sinks partially into maze 0 Answers

[Help] I want my parent's rigidbody to be unaffected by the child's collisions. 0 Answers

Static Collider.Move warning is still in place: should I add Rigidbody or not? (Unity 5.3.3f1) 0 Answers

Player passing through walls when in corners 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