My objects can only move if rotation is on, with what seems like a friction issue.
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!
Your answer
Follow this Question
Related Questions
Friction affects ball's direction after collision 0 Answers
Best possible way to implement physics on a chariot 2 Answers
Jumping and collision issue 1 Answer
How to stop movement script on void start and resume after. 0 Answers
Issue with physic engine 0 Answers