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 CMTV · Oct 05, 2014 at 04:39 PM · rotationcontrollersphereroll

Sphere rotation in moving direction

Hello!

I am creating a 2D game and I have a "player-shpere" in it. My sphere above the surface (coordinates x = 0, y = 0, z = -1).

alt text alt text

And I have a player controller:

     void Update()
         {
             float verticalSpeed = Input.GetAxis("Vertical") * speed;
             verticalSpeed *= Time.deltaTime;
             float horizontalSpeed = Input.GetAxis("Horizontal") * speed;
             horizontalSpeed *= Time.deltaTime;
             this.transform.Translate(horizontalSpeed, verticalSpeed, 0);
         }

It works fine, but I want my sphere to roll in movig direction (like a real boll is rolling in it's direction). The sphere just have to rotate according "controller" directoin.

What should I do?

безымянный.png (120.4 kB)
безымянный.png (49.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mouurusai · Oct 05, 2014 at 06:37 PM

It's should rotate if you use physics function to move, I think. Anyway you may use something like: using UnityEngine;

 public class NewBehaviourScript : MonoBehaviour
 {
     public float r=0.5f;
     public float speed = 3;
     
     
     void FixedUpdate()
     {
         float moveDir = Input.GetAxis("Horizontal")*speed*Time.deltaTime;
 
         transform.Translate(moveDir, 0, 0, Space.World);
 
         transform.RotateAround(transform.position, Vector3.forward, -Mathf.Sin(moveDir*r*2*Mathf.PI)*Mathf.Rad2Deg);
     }
 }
 
Comment
Add comment · Show 4 · 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 CMTV · Oct 05, 2014 at 06:45 PM 0
Share

It gives me a very strange result. The sphere rotate very fast and fly somewhere. Anyway, I have updated my question.

avatar image mouurusai · Oct 05, 2014 at 07:03 PM 0
Share

transform.Translate(бла-бла ,Space.World) using UnityEngine;

 public class NewBehaviourScript : $$anonymous$$onoBehaviour
 {
     public float r=0.5f;
     public float speed = 3;
     
     
     void FixedUpdate()
     {
         Vector3 moveDelta = new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
 
         transform.Translate(moveDelta, Space.World);
 
         Vector3 rotationAxis = Vector3.Cross(moveDelta.normalized, Vector3.up);
 
         transform.RotateAround(transform.position, rotationAxis, -$$anonymous$$athf.Sin(moveDelta.magnitude*r*2*$$anonymous$$athf.PI)*$$anonymous$$athf.Rad2Deg);
     }
 }
 
avatar image CMTV · Oct 06, 2014 at 04:46 AM 0
Share

Yes it works! But when I am pressing A or D buttons, sphere starting to roll incorrect (red arrows on picture, it should roll according orange arrow).

alt text

безымянный.png (31.3 kB)
avatar image mouurusai · Oct 06, 2014 at 11:33 AM 0
Share

It's because you are use axises different from axises in my example.

 using UnityEngine;
 
 
 public class NewBehaviourScript : $$anonymous$$onoBehaviour
 {
     public float r=0.5f;
     public float speed = 3;
     
     
     void FixedUpdate()
     {
         Vector3 moveDelta = new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0);
 
         transform.Translate(moveDelta, Space.World);
 
         Vector3 rotationAxis = Vector3.Cross(moveDelta.normalized, Vector3.forward);
 
         transform.RotateAround(transform.position, rotationAxis, $$anonymous$$athf.Sin(moveDelta.magnitude*r*2*$$anonymous$$athf.PI)*$$anonymous$$athf.Rad2Deg);
     }
 }
 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Make sphere rotate when controlled 7 Answers

click to move workig script!! but pls help with rotation!! :( 1 Answer

2D sprite rotation with velocity and interpolation 1 Answer

Problem in rotation of plane 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