• 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 zapasowyrowerek · Mar 20, 2020 at 08:34 PM · movementphysics3drotatesphere

Move sphere with independent orientation

Hello. I have problem with writing move scrpit for player character which is sphere. I want to move it independently from global orientation and independently from sphere transform. Let me give an example:

If I use AddRelativeTorque/Force sphere`s transform will be rotating and because of that It will be impossible to move it anymore. If I bind "W" to move forward and then press it, sphere might roll in left direction or sth like that.

If I use AddTorque I will be only able to move along global axes, so it is impossible to turn back with only 2 keys ("W" + "D" for example)

I tried to experiment with empty gameobject which contain sphere. I thought I can move sphere with adding force to empty object and then rotate object with velocity (transform.rotation.SetLookRotation()) but still no results.

My last script is very close to my idea, but there is 2 problems. 1. Rotation for X and Z axes must be frozen. 2. Game object don`t rotate with velocity, when I rotate it manually it works as I want.

 public class move_character : MonoBehaviour
 {
     public float speed = 1f;
     private Rigidbody rb;
     private Vector3 torque_vector_x = new Vector3(1, 0, 0);
     private Vector3 torque_vector_z = new Vector3(0, 0, 1);
 
     private void move_forward()
     {
         rb.AddRelativeForce(torque_vector_x * speed * Time.deltaTime);
     }
 
     private void move_back()
     {
         rb.AddRelativeForce(torque_vector_x * speed * Time.deltaTime * (-1));
     }
 
     private void move_left()
     {
         rb.AddRelativeForce(torque_vector_z * speed * Time.deltaTime);
     }
 
     private void move_right()
     {
         rb.AddRelativeForce(torque_vector_z * speed * Time.deltaTime * (-1));
     }
 
 
     void Start()
     {
         rb = gameObject.GetComponent<Rigidbody>();
     }
 
     
     void Update()
     {
         transform.rotation.SetLookRotation(rb.velocity);
         if (Input.GetKey(KeyCode.W))
         {
             move_forward();
         }
         if (Input.GetKey(KeyCode.S))
         {
             move_back();
         }
         if (Input.GetKey(KeyCode.A))
         {
             move_left();
         }
         if (Input.GetKey(KeyCode.D))
         {
             move_right();
         }
     }
 }

alt text

Anyone has any idea how can I make this ball moving as I described?

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 BBIT-SOLUTIONS · Mar 20, 2020 at 08:52 PM

Did you try to move only the rigidbody on your sphere, but leave the script on the GameObject? In this case, the Axes should be frozen.

As a little tip: Instead of defining your both vectors, you could just use Vector3.right and Vector3.forward instead. That could simplify your script maybe a bit.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Objects stuck dragging across floor 1 Answer

Making a sphere move and rotate like a real ball on ground 2 Answers

Can't fix slow jumping for character controller 3D 1 Answer

How to apply equal force back to player when colliding with a wall? 2 Answers

Why my sphere fly away? 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