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 /
This question was closed Jul 11, 2021 at 06:33 AM by iR3dyPlayZ.
avatar image
0
Question by iR3dyPlayZ · Jul 10, 2021 at 06:40 PM · movementrigidbodyraycastcollision detection

Rigidbody 3D Sticking issue

How would I go about writing a code that checks for wall collisions and says hey if the rigidbody hits wall, the player rigidbody can no longer move towards the wall and must move left or right in either axis (x,z).

Also I don't want to use physics material because it will clash with future plans...

-Also im having issues with my player cords going into scientific notation which affects my jumping in some instances.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class Player : MonoBehaviour
 {
     public float movementVelocity;
     Vector3 forward;
 
     public float rotationSpeed;
     Quaternion rotate;
 
     public float jumpVelocity;
     public float fallMultiplier;
     public float lowerJumpMultiplier;
 
     private bool isGrounded;
 
     public Rigidbody playerRigidbody;
 
     void Start()
     {
         playerRigidbody = GetComponent<Rigidbody>();
     }
     //-----//
     void Update()
     {
         _Jump();
     }
     //-----//
     void FixedUpdate()
     {
         Move();
         _Rotate();
     }
     //-----//
     void Move()
     {
         forward.x = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
         forward.z = Input.GetAxisRaw("Vertical") * Time.deltaTime;
 
         var velocity = new Vector3(forward.x, 0, forward.z).normalized * movementVelocity;
         velocity.y = playerRigidbody.velocity.y;
         playerRigidbody.velocity = velocity;
     }
     //-----//
     void _Rotate()
     {
         if (forward != Vector3.zero)
         {
             rotate = Quaternion.LookRotation(forward, Vector3.up);
 
             playerRigidbody.rotation = Quaternion.RotateTowards(playerRigidbody.rotation, rotate, rotationSpeed);
         }
     }
     //-----//
     void _Jump()
         // "Better Jump" Provided By: Board To Bits Games //
     {
 
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             playerRigidbody.velocity = Vector3.up * jumpVelocity;
             isGrounded = false;
         }
 
         if (playerRigidbody.velocity.y < 0)
         {
             playerRigidbody.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
         }
 
         else if (playerRigidbody.velocity.y > 0 && !Input.GetButton("Jump"))
         {
             playerRigidbody.velocity += Vector3.up * Physics.gravity.y * (lowerJumpMultiplier - 1) * Time.deltaTime;
         }
     }
     //-----//
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "Ground")
             isGrounded = true;
     }
 
 }
 
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

2 Replies

  • Sort: 
avatar image
0

Answer by Zottic · Jul 10, 2021 at 07:59 PM

Try to add a physics material to the wall with friction near 0.

Comment
Add comment · Show 2 · 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
avatar image iR3dyPlayZ · Jul 10, 2021 at 08:06 PM 0
Share

I cant use Physics material

avatar image iR3dyPlayZ · Jul 10, 2021 at 09:08 PM 0
Share

Also not all walls are walls, some are elevated platforms with side which are acting like walls, but its still labeled as the ground

avatar image
0

Answer by iR3dyPlayZ · Jul 10, 2021 at 08:52 PM

Okay so i found code monkeys tutorial for movement and wall detection, but he uses

  Raycasthit raycasthit = Physics.Raycast(transform.postion);

and I'm using a rigidbody, but im not able to use

 Raycasthit raycasthit = Physics.Raycast(playerRigidbody.velocity = new Vector3(forward.x, 0, forward.z)).normalized * movementVelocity;
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

120 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

Related Questions

Raycasting in 2D to prevent a player from moving through walls? 1 Answer

Problem with softbodies collisions 0 Answers

How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers

Problem with collision detection during animation 1 Answer

Why can't i addforce to a rigidbody? 2 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