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 /
avatar image
0
Question by oneslyfox · Mar 20, 2012 at 03:21 PM · rotationraycastgravitylocalflip

Get raycast to ignore rotational axis

In my sidescroller game the character's Z rotation is based on a raycast pointing from -transform.up allowing him to rotate along convex/concave surfaces (and walk up walls and along ceilings). I'm creating my own gravity that always pulls the character towards any surface that's below him (locally) and the character rotation is always based on the normal of the surface the raycast is hitting.

This works great, however, one of the key mechanics for my game will be to allow the player to flip up/down from ceiling/floor or flip left/right from right wall/left wall.

When I rotate the X axis to make the character flip to another surface the raycast will angle diagonally off the terrain causing some wild spinning and rotations.

My question boils down to this:

Is it possible to have the raycast not rotate with the player's X rotation? Just the Z?

Hope I explained this well. I new to Unity. Loving it so far!

Thanks!

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

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

Answer by ByteSheep · Mar 20, 2012 at 04:08 PM

Since you're probaly using -transform.up on the character, the raycast will always be facing downwards according to the character.. My first thought would be to make the character a child object of a new empty object and give the new object the raycast instead. That way the character can flip etc without affecting the raycast, but the raycast position will still follow the character (if you move the new object instead of the character). Another idea would be to disable the ray while the character is flipping, as to avoid the strange spinning ;)

Comment
Add comment · Show 3 · 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 oneslyfox · Mar 20, 2012 at 07:12 PM 0
Share

I originally tried that, but I would still get wacky results... However, after you mentioned this, I revisited that as a solution and what I came up with worked.

$$anonymous$$y original, similar solution, was instantly changing the rotation of the player so the raycast would flip and detect the opposite walls/floors. Then, I would change the child character model's rotation accordingly. However, this did not work; still flip flopping all over the place.

Solution: ins$$anonymous$$d of rotating the player I made a variable called "flipDir" that's either 1 or -1 and then multipled it to the raycast direction, gravity, and jump direction when the flip button is pressed.

Now, I have a new problem... Not sure how to do a smooth local rotation of the child character model within player parent. Seems to always be global rotation. However, no matter what funky rotation I get, at least the parent character model is rotating and behaving properly now.

Thanks!

avatar image oneslyfox · Mar 20, 2012 at 10:08 PM 0
Share

var newRot = transform.localRotation * Quaternion.Euler(180,0,0); chara.transform.rotation = Quaternion.Slerp(chara.transform.rotation, newRot, Time.deltaTime * 2);

That fixed the rotation.

I'm confused about something though. How come this code:

var newRot = transform.localRotation * Quaternion.Euler(180,0,0);

Is not the same as this code:

var newRot = Quaternion.Euler(180,0,0) * transform.localRotation;

avatar image ByteSheep · Mar 20, 2012 at 10:10 PM 0
Share

I see no difference? :P

avatar image
0

Answer by rutter · Mar 20, 2012 at 06:39 PM

After grabbing -transform.up, you could zero out a particular axis before building your raycast.

For example:

 var dir = -transform.up;
 dir.z = 0f;
 var ray = new Ray(transform.position, dir);
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 oneslyfox · Mar 20, 2012 at 08:34 PM 0
Share

That would work too, but I think the first answer will give me better control of my character animations and the collision of the parent player object.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to keep local rotation when using raycasts? 1 Answer

Flip over an object (smooth transition) 3 Answers

How to get Ray rotation from direction 0 Answers

Detecting 360 degree spin on local y axis 2 Answers

Detect if on ground? 1 Answer


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