- Home /
sonic movement stick to the ground
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
Answer by SunnyChow · Jan 24, 2018 at 10:45 AM
after "transform.position = new Vector3 blah blah blah;", add "transform.up = hit.normal;".
it works partly, like it does rotate but now it teleports
Your answer
Follow this Question
Related Questions
Smooth movement between two exact locations 0 Answers
how to make automatic gradual acceleration 1 Answer
How can i make my character jump and slide on command? 0 Answers
My movement script is kind of broken for jumping (upward force is diminished after first jump) 1 Answer
How to make a top down character snap to different directions 0 Answers