Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by mkjrfan · Jan 21, 2013 at 06:15 PM · 2dplatformrotatingskateboard

Rotating based on the platform the player is standing on?

I am trying to make a 2d skateboarding game and I need to make the player rotate based on the rotation of the block under neath the player. This way skating down a ramp the board is on the ground

(I am kind of new and this is how I would think of it) Basically I was thinking of casting a ray or somthing that would detect the platform underneath the player. Then it would then find the rotation of that platform and rotate the player perpendicular to that.

Does anyone know how to find the rotation of an object underneath a player object?

Comment
Add comment · Show 21
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 sparkzbarca · Jan 22, 2013 at 03:01 AM 0
Share

comment don't make that an answer.

cast a ray from player.transform down.

take the inverse(-) of the normal of the surface you hit use those to find the axis of rotation and the angle, I know you've no idea how to find those with just that information so i've posted the code.

 axis = vector3.cross(-player.transform.up,-hit.normal);
 if(axis != vector3.zero)
 angle = atan2(vector3.magnitude(axis), vector3.dot(-player.transform.up,-hit.normal));
 
 player.transform.rota$$anonymous$$round(axis,angle);

this will rotate the character so his feet are always parallel with the ground. mark as answered and have a nice day

avatar image samz · Jan 22, 2013 at 04:19 PM 0
Share

THAN$$anonymous$$ YOUUU...this works great. Just one tinny little issue. Would i be able to rotate the player slowly when its is moving slowly against a curvy platform? it rotate very quick and sharp atm if you can understand what im tryna say??

avatar image sparkzbarca · Jan 22, 2013 at 06:28 PM 0
Share

use slerp to rotate over time ins$$anonymous$$d of instantly.

avatar image mkjrfan · Jan 22, 2013 at 08:41 PM 0
Share

Thank you for replying and ya sorry about submitting that as a an anwser. I don't have time to look at it today because I have tests to study for, but once I look at it and if it works I will make it the anwser.

avatar image mkjrfan · Jan 23, 2013 at 09:55 PM 0
Share

what is hit supposed to be?

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by mkjrfan · Jan 25, 2013 at 09:06 PM

No idea why but for some reason sparkzbarca's anwser became a comment and now neither I nor anyone else can see the rest of the conversation even if you coment on it. This is what he wrote as the anwser: Ray ray = new Ray(); RaycastHit hit; Vector3 axis; float angle;

     ray.origin = transform.position;
     ray.direction = -transform.up;
 
     Physics.Raycast(ray, out hit);
 
     axis = Vector3.Cross(-transform.up,-hit.normal);
     if(axis != Vector3.zero)
            {
     angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up,-hit.normal));
     transform.RotateAround(axis,angle);
            }

it works.

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 maxisbestest · Apr 03, 2019 at 06:21 PM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Collide : $$anonymous$$onoBehaviour { Ray ray = new Ray(); RaycastHit hit; float angle; public bool isTurning; Quaternion pastRot; float rotX; float rotY; float rotZ;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {

     pastRot = this.transform.rotation;

     ray.origin = transform.position;
     ray.direction = -transform.up;

     Physics.Raycast(ray, out hit);

     rotX = hit.transform.rotation.x - transform.rotation.x;
     rotZ = hit.transform.rotation.z - transform.rotation.z;

     this.transform.rotation *= Quaternion.Euler(rotX * 100 * Time.deltaTime, rotY * 100 * Time.deltaTime, rotZ * 100 * Time.deltaTime);
     transform.parent = hit.transform;

     if (this.transform.rotation != pastRot)
     {
         isTurning = true;
     }

     if (this.transform.rotation == pastRot)
     {
         isTurning = false;
     }

 }
    

}

This is my code. I used sparkzbarcas and it ended up rotating on the y axis if the x and z axis were both non-zero values for me.

avatar image
0

Answer by nantoaqui · Dec 12, 2017 at 11:22 PM

Hello!!

Works perfectly! I would like to know why are we using Vector3.Dot in this case to get the x position?

Thank you!

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
avatar image
0

Answer by maxisbestest · Mar 29, 2019 at 11:59 AM

Thanks so much this is really helpful!

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

Spinning rigidbody platform in 2D 0 Answers

Setting Velocity of Rigidbody to Mouse Speed 1 Answer

Problem with rotated colliders 0 Answers

Simple Topdown Movement Problem 1 Answer

getchild(0), but first child is gonna be destroyed! 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