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 Jakoul · Jun 03, 2012 at 01:25 AM · c#transformraycasting

How To Move Object To Terrain Slope in C# With Raycast?

I am trying to get a object to the same x and z as the terrain below it. My method right now is to get Raycast to get the normal and then trying to mess with transform.eulerAngles until they are the same. I fear this might be a little over my head though as I cannot figure this out for the life of me and I have been trying for several hours.

using UnityEngine; using System.Collections;

public class example : MonoBehaviour { public Vector3 hitr; public Vector3 hitrn;

 void Update() {
     RaycastHit hit;
     Physics.Raycast(transform.position, Vector3.down, out hit);
     if (Physics.Raycast(transform.position, Vector3.down, 2)){
         hitr = hit.normal;
         hitrn = hitr - transform.eulerAngles;
         transform.Rotate(hitrn.x, 0, hitrn.z);
     }
 }

}

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
2
Best Answer

Answer by whydoidoit · Jun 03, 2012 at 10:22 AM

I see that you have two choices:

  • Presuming the normal coming from the raycast hit is what you need (it looks right when you use it):

    transform.up = hit.normal;

  • If that doesn't do it, it might be better to sample a couple of points around the base of your model and then work out the slope angle from that

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 Jakoul · Jun 03, 2012 at 11:18 AM 0
Share

That does exactly what I wanted to do it. Can't believe I missed something so simple! Thank you very very much!

avatar image whydoidoit · Jun 03, 2012 at 11:29 AM 0
Share

You are more than welcome! Please mark the question as answered.

And hey - I spent hours the other day before I remembered I could do that...

avatar image
0

Answer by Tasarran · Jun 03, 2012 at 03:00 AM

Look in the docs at this component of the hit...

http://unity3d.com/support/documentation/ScriptReference/RaycastHit-barycentricCoordinate.html

That script there draws a Debug Line in the editor window, but you can modify it to do what you want.

Comment
Add comment · Show 5 · 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 Jakoul · Jun 03, 2012 at 06:22 AM 0
Share

I'm not trying to move along the X/Z world grid. I am trying to change my object's rotation to the same as the terrain below it.

avatar image whydoidoit · Jun 03, 2012 at 08:51 AM 0
Share

Can you explain the effect you want? Are you looking to rotate the object to fit the slope of the terrain?

avatar image Jakoul · Jun 03, 2012 at 08:59 AM 0
Share

Yes! Generic as it is, it is for a basic tank character. It seems and feels like there is some variable or function I am missing to make this work.

avatar image whydoidoit · Jun 03, 2012 at 10:21 AM 0
Share

Ok so then you are trying to rotate the green axis of your model to the normal you get from the terrain, I get it. These calculations are always a nightmare :)

avatar image devstudents · Nov 15, 2014 at 01:53 AM 0
Share

Sorry to resurrect an old thread. I'm trying to do the same thing but for building instantiation. I want the player to be able to build on slight slopes. At the moment part of the building cuts through the terrain. If anyone can help out, let me know and I'll post my code.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Code working on wrong axis. 1 Answer

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers

placing an object to it's initital position after picked up. 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