• 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 cw4ed6 · Oct 26, 2012 at 12:02 AM · rigidbodygravityspherefrictionrolling

How to have a sphere roll/be affected by friction

Hi all. I'm hoping someone can help with the following issue. I am attempting to code a ball which can move around a 3D level. At the moment, i have it coded that the ball can move and be turned, albeit not perfectly.

There are several issues I cannot seem to resolve:

1) When moving around the level, the ball seems to shake a lot rather than moving smoothly. 2) When rotating left or right, this is not done on the spot; rather it seems to turn in an arc for some reason. 3) I cannot seem to get the ball to roll, no matter what I try.

I thought that if I applied a rigidbody to the ball and applied physics materials to both the ball and the surface, then the engine would handle the physics for me and the ball would roll when it was pushed forward. however this does not seem to work. can anyone please offer some advice on these issues?

I should also explain that I am using the smooth follow script for the main camera. its target is an empty game object which is positioned above and behind the ball. Here is my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterMovement : MonoBehaviour {
     
     
         //DECLARE MEMBER VARIABLES WITH FULL SCOPE
     
         //Variable to verify that the character can be moved
         bool controllable = true;
         //Variable to verify that the character is on the ground
         bool grounded = false;
         
         //Variables to determine the speed of movements
         float speed = 20.0f;
         float rotationSpeed = 2.0f;
         float jumpDistance = 15.0f;
         
         //Variables to set the direction of movement and rotation
         Vector3 moveDirection = Vector3.zero;
         Vector3 rotationDirection = Vector3.zero;
         
         //Variable to set the rotation value
         float horizontalMovement = 0.0f;
             
         //Variable to set gravity
         float gravity = 30.0f;
     
     
     // Use this for initialization
     void Start () {
             
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     
     // Fixed Update is called after Update has finished
     void FixedUpdate()    {
         
         //Cache the character controller
         CharacterController controller = GetComponent<CharacterController>();
         
         
         //if the character cannot be controlled, reset the point of movement
         if(controllable == false)
         {
             Input.ResetInputAxes();
         }
         else
         {
             //Check if the character is on the ground
             if(grounded)
             {
                 //Determine the movement based on the input
                 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis ("Vertical"));
                 
                 //Use the new direction/vector to enable the character position to be amended
                 moveDirection = transform.TransformDirection(moveDirection);
                 //moveDirection = Camera.main.transform.TransformDirection(moveDirection);
                 
                 //Use the vector in conjunction with the speed variable to cause movement
                 moveDirection *= speed;
                 
                 //Determine the rotation
                 horizontalMovement = Input.GetAxis("Horizontal");
                 
                 //Determine which direction the character should rotate around the y axis
                 if(horizontalMovement > 0)
                     //Rotate to the right, ie by 1 around the y-axis (positive value rotates right)
                     rotationDirection = new Vector3(0, 1, 0);
                 else if(horizontalMovement < 0)
                     //Rotate to the left, ie by -1 around the y-axis (negative value rotates left)
                     rotationDirection = new Vector3(0, -1, 0);
                 else
                     //If no change then value will remain at 0, so no rotation occurs in either direction
                     rotationDirection = new Vector3(0, 0, 0);
                 
                 if(Input.GetButton("Jump"))
                     //Raise the height of the character
                     moveDirection.y = jumpDistance;
                         
             }    
                     
             //Apply gravity to ensure a return to ground if airborne
             //This is done by decreasing the y value using the gravity factor over time
             moveDirection.y -= gravity * Time.deltaTime;
             
             //Code to move the controller and hence the character its connected to
             CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
             controller.transform.Rotate(rotationDirection * Time.deltaTime, rotationSpeed);
             grounded = ((flags & CollisionFlags.CollidedBelow) != 0);
             
         }
             
     }
     
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Wrong gravity calculation? 2 Answers

Sphere + rigidbody + character controller 1 Answer

Sphere friction? 1 Answer

Movement problem? 0 Answers

Prevent rigidbody with hinge joint from slowing down 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges