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 Arunabh Das · Dec 24, 2010 at 10:27 AM · rotatetiltslope

How to rotate/tilt an object to match angle of slope?

The title says it all. Here is my code(In C#)

float spinSpeed = 2000f; float moveSpeed; float gravity = 40f; float moveLimit; RaycastHit hit; Vector3 moveDirection = Vector3.zero; CharacterController controller;

// Use this for initialization void Start () { controller = GetComponent<CharacterController>(); animation.wrapMode = WrapMode.Loop; animation["Take 001"].speed = spinSpeed; animation.CrossFade("Take 001"); moveLimit = controller.slopeLimit - 0.1f; }

// Update is called once per frame void Update() { if (Input.GetAxis("Horizontal") > 0.1) moveSpeed = 15f; else if (Input.GetAxis("Horizontal") < -0.1) moveSpeed = 15f; else if (Input.GetAxis("Vertical") > 0.1) moveSpeed = 15f; else if (Input.GetAxis("Vertical") < -0.1) moveSpeed = 15f; else moveSpeed = 0f;

 if (Vector3.Angle(hit.normal, Vector3.up) &gt; moveLimit)
     transform.rotation =
         Quaternion.FromToRotation(Vector3.up, hit.normal) * Quaternion.LookRotation(moveDirection);

 moveDirection = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
 moveDirection = transform.TransformDirection(moveDirection);

 moveDirection.y -= gravity * Time.deltaTime;
 controller.Move(moveDirection * (Time.deltaTime * moveSpeed));

}

Comment
Add comment · Show 1
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 Jesse Anders · Dec 24, 2010 at 05:17 PM 0
Share

You're using the 'hit' variable, but you never actually perform a raycast (at least not in the code you posted). Is that intentional?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Arunabh Das · Dec 26, 2010 at 05:04 AM

I don't really get what you are talking about. Actually I never worked with raycast in my game.

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 Owen-Reynolds · May 05, 2011 at 06:48 AM 0
Share

At the top, you have a variable RaycastHit hit;. Its only goal in life is get the data back from a RayCast. You are using hit in the code, but it is all zeros, since you don't do a Raycast, which is the only way to put useful data into hit. Double-check where you found this -- you probably missed a line or two.

avatar image
0

Answer by user-10163 (google) · Mar 11, 2011 at 01:03 AM

The above script does not work as desire, could you post more information how to use the code?

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

1 Person is following this question.

avatar image

Related Questions

Tilt an image by the X axis? 0 Answers

Is there a better way to tilt terrain at runtime? 1 Answer

How to rotate terrain or tilt it for z axis when play mode? (C#) just think of it as an object 0 Answers

RTS Camera Zoom w/ Rotation 0 Answers

Add tilt function to rotate a ball android device 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