- Home /
My objects are not able to move unless rotation is on
Hi community.
I am fairly new to unity, so I apologise if the solution to this problem is obvious or if my formatting is poor. I am designing a simple code to move my characters along the ground and allow them to jump. However, when they are in contact with the ground, they are unable to move properly. Ideally, the objects should slide across the ground, neglecting friction and without rotation.
I have been testing with three objects:
A sphere with rotation restricted
A sphere with rotation allowed
A cube with rotation allowed
The sphere with rotation restricted only moves at the same speed as the other objects in the air, barely moving on the ground.
The sphere with rotation allowed rolls across the ground perfectly.
The cube flips around the ground like a dice, but in the air does not rotate.
I have tried creating a custom physics material with zero friction for my ground, yet that does not seem to work. Please refer to the code sample and the image below for a better representation of the situation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AltMotion : MonoBehaviour
{
public float jumpVelocity = 100f;
public float speed = 10f;
private Rigidbody rb;
private float velocityY;
public float gravity = -9.8f;
private bool isGrounded = false;
private Vector3 motion;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Jumping
if (Input.GetButtonDown("Jump"))
{
velocityY = Mathf.Sqrt(jumpVelocity);
}
velocityY += gravity * Time.deltaTime;
//Moving
float xMove = Input.GetAxis("Horizontal") * speed;
float zMove = Input.GetAxis("Vertical") * speed;
motion = new Vector3(xMove, velocityY, zMove);
if (motion != Vector3.zero)
{
rb.velocity = new Vector3(xMove, velocityY, zMove);
Debug.Log("x value is " + xMove + " and z value is " + zMove);
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Environment")
{
isGrounded = true;
Debug.Log(isGrounded);
}
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "Environment")
{
isGrounded = false;
Debug.Log(isGrounded);
}
}
}
Thank you for your time!
Edit: I realised that the material properties needed to be applied to both the ground and the object itself for no friction between them, hence allowing them to slide together.
Can I see a screenshot possibly of the values in your physics material? If the values nullify friction (friction is zero) then you won't need to freeze rotation to prevent the cube from rolling like a dice. @$$anonymous$$_terrarian
Hi @Llama_w_2Ls , I realised that the physics material needed to be applied to both the ground and the object, rather than just the ground. Thanks for your help!
Answer by Alex_terrarian · Dec 21, 2020 at 09:21 PM
I realised that both the ground and the object needed the zero-friction physics material to slide across the ground. This allows for smooth movement across the ground, without the need for rotation.