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
1
Question by abi-kr01 · Aug 07, 2013 at 01:04 PM · rotationslopetilting

slope detection

i am creating a game where my character called "hero" is running on a Terrain. when i was using a plain train it was working fine ,problem starts when i want to run the same character on slopes i have box colider in both of legs of character. during slope ,first portion of colider help my character to run. but running is not natural because on slope it must tilt little bit and that is not happening code is as follows

 static var monster : GameObject ;
 private var ray : Ray;
 private var hit : RaycastHit;
 static var speed : int;
    
 function Start () 
 {
 monster =   GameObject.Find("hero");
 speed= 10;
 }
 function Update ()
 {
  monster.rigidbody.velocity.x = speed; 
  
 }
 
 //collision detection
 var isCollideTurning : boolean;
  isCollideTurning = true;
  
  function OnCollisionEnter ()
  {
 monster.animation.Play("run");
   }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by robertbu · Aug 07, 2013 at 03:23 PM

Raycasting() is used to detect the slope. If you are going just for the slope of the terrain, then I suggest you use Colider.Raycast() rather than Physics.Raycast(). Sample code:

 var hit : RaycastHit;
 var ray = new Ray(transform.position, Vector3.down);
 if (someCollider.Raycast(ray, hit)) {
   var slope = hit.normal;
   //Adjust character based on normal
 }

This gives you back the normal to the slope:

alt text

Your question was unclear about how you wanted to adjust the rotation of your character. A lot of scripts I see here adjust the object to align with the normal. You can do that by:

 transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;



surface_normal.png (361.7 kB)
Comment
Add comment · Show 6 · 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 abi-kr01 · Aug 08, 2013 at 06:54 AM 0
Share

i am having the problem with the object and train ,for example when u drive the car in hills ur car is not parallel (it tilt according to slope/angle) with ur ground as compare to normal plane driving i have uploaded the video just check it out and plz help u can see player is running straight

avatar image robertbu · Aug 08, 2013 at 07:01 AM 0
Share

Did you add the code I suggested? Putting both pieces together it would be:

 var hit : RaycastHit;
 var ray = new Ray(transform.position, Vector3.down);
 if (someCollider.Raycast(ray, hit, 1000)) {
      transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
 }

Where someCollider is a reference to the collider on your terrain.

avatar image abi-kr01 · Aug 08, 2013 at 10:34 AM 0
Share

there is error inn the if statement where u told me to put the terrain collide saying

An instance of type 'UnityEngine.colider' is required to access non static member 'raycast'.

what is the problem

avatar image robertbu · Aug 08, 2013 at 01:41 PM 0
Share

At the top of your script, put:

 var someCollider : Collider;

Then in the inspector, drag the terrain onto this variable.

avatar image abi-kr01 · Aug 09, 2013 at 07:53 AM 0
Share

The best overload for the method 'UnityEngine.Collider.Raycast(UnityEngine.Ray, ref UnityEngine.RaycastHit, float)' is not compatible with the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit)'.

can u give me ur mail id so that i can chat with u this is very $$anonymous$$or problem and m stuck in it for 2 days my project will not go any further until this problem is solved

Show more comments
avatar image
0

Answer by Xtro · Aug 07, 2013 at 02:00 PM

I think you should use capsule collider. To prevent you character to laydown, you can freeze the rotation on selected axes.

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 abi-kr01 · Sep 03, 2013 at 10:17 AM 0
Share

thanks Xtro but when we increase the drag in the rigidbody component ewverything works fine

avatar image
0

Answer by Saddamjit_Singh · Sep 04, 2017 at 09:25 AM

This code is having a problem.

transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) transform.rotation*;

multiplying the equation with transform.rotation changes y axis of gameobject slowly over time.

@robertbu

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

16 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

Related Questions

Sliding game slope jittering. 0 Answers

Gyroscope Tilting Rotation. Quaternions T_T Help. 1 Answer

Maze game style rotation 1 Answer

Skateboarding Rotation Problem 1 Answer

Align child object to grounds normal(slopes) 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