• 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 JoshMBeyer · Mar 27, 2014 at 07:03 PM · rigidbodyrotatemove

How to rotate/move diagnal with rigidbody sphere

Having trouble with the physics here. I want the player(sphere game object) to be able to roll in every direction to navigate through the platform course. I can now go back and forth but trying to go to the side or diagnal isnt working properly here is the script. What am I doing wrong exactly?

  UnityEngine;
 using System.Collections;
 
 public class SphereController : MonoBehaviour {
 
 
     //The constant forward speed.
     public float sphereSpeed;
     //The user controlled left and right speed.
     public float leftRightSpeed;
 
     //Win or Loss.
     public bool win;
     public bool gameOver;
 
     //The visual partical effect for when we collide.
     //Just a cheap example.
     public GameObject prefabVisual;
     
     
     void Update () 
     {
         rigidbody.AddForce(Input.GetAxis("Horizontal"), 0, 0 * leftRightSpeed);
         //transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal"),0));
         rigidbody.AddForce(0, 0, Input.GetAxis("Vertical") * sphereSpeed);
         //Move forwards or backwards.
         
 
         if (transform.position.z >= 19)
         {
             win = true;
             //Disable movement of the sphere.
             //Here we could load a different scene. Win screen, highscore screen, etc.
             this.gameObject.GetComponent<SphereController>().enabled = false;
         }
     }
 
     void OnCollisionEnter(Collision other)
     {
         //If we hit a obstacle.
         if (other.gameObject.tag == ("Obstacle"))
         {
             //Tell us what we hit.
             Debug.Log("Hit an obstical" + other.gameObject);
             
             //gameOver = true;
 
             //Instantiate the prefabVisual.
             Instantiate(prefabVisual,transform.position,transform.rotation);
             
             //Disable movement of the sphere.
             this.gameObject.GetComponent<SphereController>().enabled = false;
             
             //Make the sphere dissapear.
             this.gameObject.GetComponent<MeshRenderer>().enabled = false;
             //Could add a game over scene now.
         }
     }
 }
 
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
Best Answer

Answer by T27M · Mar 27, 2014 at 07:20 PM

You have a few variables in the wrong place, make sure you set the values of sphereSpeed and leftRightSpeed in the inspector.

GetAxis returns either -1, 0, or 1. You are taking that value and multiplying it by your speed to move. Also check the FixedUpdate function, when dealing with forces it's recommended to use FixedUpdate.

     //The constant forward speed.
     public float sphereSpeed = 1;
     //The user controlled left and right speed.
     public float leftRightSpeed = 1;
 
         
     // Update is called once per frame
     void FixedUpdate () {
         rigidbody.AddForce(Input.GetAxis("Horizontal") * leftRightSpeed, 0, 0);
 
         rigidbody.AddForce(0, 0, Input.GetAxis("Vertical") * sphereSpeed);
     }
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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Move a rigid body in an area of a platform 0 Answers

Rotate an Object according to a GUI? 0 Answers

Gravity only rigidbody 1 Answer

Google VR Play Store sample app built using Unity 3D with move and rotate 1 Answer

Rotate multiple objects around or rather move them on the surface of another object 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