- Home /
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);
}
}
}
Your answer
Follow this Question
Related Questions
Wrong gravity calculation? 2 Answers
slide sphere on a slope without rotating 1 Answer
Sphere + rigidbody + character controller 1 Answer
Prevent rigidbody with hinge joint from slowing down 1 Answer
Sphere friction? 1 Answer