Sooo for a school project i’m making sonics movement but its not really working how it should, often its just pushing sonic through the ground and it wont rotate with the steep hils down. here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public float rotationSpeed = 100.0F;
public float speed;
private float maxSpeed = 10;
private float Speedup = 0;
private Rigidbody rb;
private Vector3 moveDir;
private GroundChecker groundCheck;
public bool freezeRotation;
private void Start()
{
rb = GetComponent();
groundCheck = GetComponentInChildren();
}
void Update()
{
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= (speed + Speedup);
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
if (moveDir.sqrMagnitude > 0.5f){
Speedup += 1;
}else{
Speedup = 0;
}
if (speed + Speedup > maxSpeed){
Speedup -= 1;
}
}
private void FixedUpdate()
{
RaycastHit hit;
if (!groundCheck.isGrounded)
{
print("raycast is Active");
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
if (Physics.Raycast(this.transform.position, -transform.up, out hit))
{
transform.position = new Vector3(transform.position.x, Mathf.Round(hit.point.y), transform.position.z);
}
}
}
}
Please someone help me asap