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 /
avatar image
0
Question by Master_Davicous · Apr 28, 2017 at 03:14 PM · raycastvector3colliders

Raycasting in direction of input, while player's gravity is relative to the surface they are standing on?

I am using a script for my player similar to this one made by @aldonaletto (C# version by @getyour411): http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html

However, the collision with walking into walls is quite buggy and the player is able to even pass through some of these colliders.

What I would like to do is raycast in the direction of the directional inputs to see if there is a collider in the way, (the raycast being the length of the player's width) and if there is a collider in the way, it will not allow the player to continue moving in that direction.

I am having trouble figuring out how to raycast in the input's direction while also having the player's rotation constantly changing because gravity is relative to the surface the player is standing on.

This is the code I have for movement input:

 leftInputHorizontal = Mathf.Clamp(controller.LeftStickX + Input.GetAxis("Horizontal"), -1f, 1f);
 leftInputVertical = Mathf.Clamp(controller.LeftStickY + Input.GetAxis("Vertical"), -1f, 1f);

The way that the player's normal is found is (from aldonaletto/getyour411's code):

 Ray groundedRay;
 RaycastHit groundedHit;
 // update surface normal and isGrounded:
 groundedRay = new Ray(myTransform.position, -myNormal); // cast ray downwards
 if (Physics.Raycast(groundedRay, out groundedHit))
 {
     // use it to update myNormal and isGrounded
     isGrounded = groundedHit.distance <= distGround + deltaGround;
     surfaceNormal = groundedHit.normal;
 }
 else
 {
     isGrounded = false;
 }
 myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);

I have a feeling that it would have something to do with the Vector3.Cross function, but I can't seem to figure it out...

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Igor_Vasiak · Apr 28, 2017 at 04:35 PM

Movements that are not based on physics get a little messy at a physics game. Use Rigidbody.AddForce() instead. Just like this: private float speed = 120000;

 private float inputV;
 private float inputH;
 
 private float moveZ;
 private float moveX;
 
 private Rigidbody rbody;
 
 void FixedUpdate
 {
      rbody = GetComponent<Rigidbody>();
 
      inputV = Input.GetAxis("Vertical");
      inputH = Input.GetAxis("Horizontal")
 
      moveZ = inputV * speed * Time.fixedDeltaTime;
      moveX = inputH * speed * Time.fixedDeltaTime;
 
      rbody.velocity = new Vector3(0, rbody.velocity.y, 0);
 
      if (inputV != 0)
           rbody.AddForce(transform.forward * moveZ * Time.deltaTime, ForceMode.Force);
 
      if (inputH != 0)
           rbody.AddForce(transform.right * moveX * Time.deltaTime, ForceMode.Force);
 }

Just make sure It's on FixedUpdate loop, since it run almost the same time as physics calcules. Hope I've helped.

Comment
Add comment · Show 1 · 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 Master_Davicous · Apr 29, 2017 at 04:27 PM 0
Share

Thanks for the help! I was using transform.Translate to move and I guess that doesn't work well with the rigidbody, so I'll use your method now! :)

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

97 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

Related Questions

RaycastHit Collider Question 1 Answer

Dynamically Change Position of RayCast 1 Answer

Raycast without collider 1 Answer

Raycast origin overlapping surface of collider counts as a hit 0 Answers

What drawback of frequently changing of Game Object's Layer? 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