Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Pixel-Sword · Jul 13, 2016 at 09:28 AM · c#rotationmovementcontroldriving

Rotating Character While Terrain Hugging

By using this post, I have made a driving character, which automatically moves forward and hugs the ground and slopes (matches its rotation). This works, but I also want my character to rotate left and right while riding the ground and slopes (see: Kirby Air Ride). Is that a possibility and how would I accomplish it?

       public Transform backLeft;
       public Transform backRight;
       public Transform frontLeft;
       public Transform frontRight;
       public RaycastHit lr;
       public RaycastHit rr;
       public RaycastHit lf;
       public RaycastHit rf;
       public Vector3 upDir;
       void Update () {
           Physics.Raycast(backLeft.position + Vector3.up, Vector3.down, out lr);
           Physics.Raycast(backRight.position + Vector3.up, Vector3.down, out rr);
           Physics.Raycast(frontLeft.position + Vector3.up, Vector3.down, out lf);
           Physics.Raycast(frontRight.position + Vector3.up, Vector3.down, out rf);
           upDir = (Vector3.Cross(rr.point - Vector3.up, lr.point - Vector3.up) +
                    Vector3.Cross(lr.point - Vector3.up, lf.point - Vector3.up) +
                    Vector3.Cross(lf.point - Vector3.up, rf.point - Vector3.up) +
                    Vector3.Cross(rf.point - Vector3.up, rr.point - Vector3.up)
                   ).normalized;
           Debug.DrawRay(rr.point, Vector3.up);
           Debug.DrawRay(lr.point, Vector3.up);
           Debug.DrawRay(lf.point, Vector3.up);
           Debug.DrawRay(rf.point, Vector3.up);
           transform.up = upDir;
       
       }

Thank you for reading, and please do reply with any help you can give.

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
1

Answer by NogeGames · Jul 13, 2016 at 01:16 PM

Ok, so: if I have understood what you want to do it should be pretty easy... Right now your character`s rotation always matches floor , and you want this rotation to be altered when the player is turning right or left. I would just do this:

1) calculate the terrain's perpendicular normal vector; it is your upDir vector which you already have.

2) create a second vector for turning, for example, if this vector is based on an analogic stick you will have a X input value between -1 (totally left) and 1 (totally right). The calculation could be something like:

 float x = "analogicInput";
 float scale = 10f;
 Vector3 inputRotation = new Vector3 (x*scale, 0,0);

3) Add both vector getting a new upVector that depends on the input and terrain. Assign transform.up to this.

Just adding this to your code should be enough (using whatever you want instead of "analogicInput" and whatever scale you want)

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 Pixel-Sword · Jul 13, 2016 at 10:52 PM 0
Share

I tried adding the code, and it is saying that it could not convert string to float. And I do not understand what you mean by adding vectors and assigning them to transform.up, wouldn't that only affect on the vertical rotation?

avatar image NogeGames · Jul 14, 2016 at 07:48 AM 0
Share

"analogicInput" was suposed to be replaced on whatever you want to make your vehicle turn (a numeric variable).

I think it would be great to learn a bit more program$$anonymous$$g (you should know what is a float and what is a string) and some geometrics/algebra, vectors are tricky.

About you not understanding the logic behind adding this vectors is not something I can quickly explain... Your transform.up can do Roll and $$anonymous$$ch rotations (x and z axis rotations) but it cannot make Yaw rotations (y axis): i guess you are trying to add Roll rotations to your terrain based rotations, sooo, you can do it adding your upDir and a new vector.

I think it will still be confusing to you... but try to google all of this if you want to learn D: ... I have studied mathematics and program$$anonymous$$g for years, and it's hard to explain some things in a "short" answer post.

avatar image BAE10 · Sep 11, 2016 at 01:48 PM 0
Share

@Noge-v im trying to do terrain hugging with this exact same script but i need my character to do Yaw rotation (y axis) , how can i do that ?

avatar image
0

Answer by Joe0321 · May 17 at 09:04 AM

check this out for solution have solved same problem here : https://answers.unity.com/questions/1215146/rotating-character-while-terrain-hugging.html/[Portal.Arise.com][1]

[1]: https://www.ariseportal.net/

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

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

7 People are following this question.

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

Related Questions

Flip over an object (smooth transition) 3 Answers

How to make camera position relative to a specific target. 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

GetKeyUp not registering 2 Answers

Why the player is not rotating and moving at the same time ? 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