- Home /
Walk always on the ground
Hello guys.
i'm looking to make able my player to stay always on the ground, for example i have a cube and my player walk on it, when is on the edge of the cube it will just to walk on the other face of it, without fall down....
there is anyway to do something like this with unity Rigidbody?
i have this code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private bool IsRotated = false;
private bool jumping = false;
private const string IdleAnimName = "Idle";
private const string WalkingAnimName = "Move";
private Animator myAnim;
private float speedRation = 2.0f;
private Vector3 surfaceNormal;
public Vector3 personalNormal;
private float personalGravity = 9.8f;
private float normalSwitchRange = 5.0f;
private Rigidbody rigidbody;
[SerializeField]
private Transform penguin;
// Start is called before the first frame update
void Start()
{
if(myAnim == null)
{
myAnim = GetComponent<Animator>();
}
if(rigidbody == null)
{
rigidbody = GetComponent<Rigidbody>();
}
personalNormal = transform.up;
rigidbody.freezeRotation = true;
}
// Update is called once per frame
void Update()
{
float deltatime = Time.deltaTime;
InMoviment(deltatime);
if(!jumping)
{
UpdatePersonalNormal();
}
}
private void InMoviment(float deltatime)
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
UpdatePlayerYrotation();
UpdateRottationBool(x);
UpdateMovement(x,y,deltatime);
if(IsRotated) {return;}
UpdateRotation(x,y);
}
void FixedUpdate()
{
rigidbody.AddForce(-personalGravity * rigidbody.mass * personalNormal);
}
private void UpdateRotation(float x, float y)
{
const float targetX= 0.1f;
const float targetXN = -0.1f;
if(x >= targetX)
{
transform.Rotate(Vector3.up,90.0f);
IsRotated = true;
}
if(x <= targetXN)
{
transform.Rotate(Vector3.up,-90.0f);
IsRotated = true;
}
}
private void UpdateRottationBool(float x)
{
const float target = 0.0f;
if(x == target)
{
IsRotated = false;
}
}
private void UpdateMovement(float x, float y,float deltatime)
{
float t = deltatime;
const float targetX= 0.1f;
//const float targetXN = -0.1f;
if(y >= targetX)
{
myAnim.Play(WalkingAnimName);
Vector3 newPos = transform.forward * y * t;
Vector3 lerpedPos = Vector3.Lerp( transform.position,newPos,t);
transform.position += newPos;
}
else if(y < targetX)
{
myAnim.Play(IdleAnimName);
}
}
public void UpdatePersonalNormal()
{
RaycastHit hit;
Vector3[] rays =
{
Vector3.up, Vector3.down,
Vector3.left, Vector3.right,
Vector3.forward, Vector3.back
};
foreach(Vector3 rayDirection in rays)
{
if(Physics.Raycast(this.transform.position , rayDirection, out hit,normalSwitchRange))
{
personalNormal = hit.normal;
}
}
}
private void UpdatePlayerYrotation()
{
const float costante = 0.0f;
Vector3 newRot;
if(personalNormal.x > costante)
{
newRot = new Vector3(0,90.0f,0.0f);
penguin.localRotation = Quaternion.Euler(newRot);
}
}
}
i'm able to change my gravity based on cube face but i have also to rotate my Character of 90 degrees when it switch cube faces, right now it will be freeze;
I did a quick search on here and the first result has a good answer.
it doesn't work on Unity 5+ but thank's you for your answer
Oh sorry. This other post has a larger thread and various solutions and explanations. This would be a good place to start:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# Movement Script 0 Answers
Distribute terrain in zones 3 Answers
Swing with rigidbody 1 Answer
(Closed)I have a Issue with rigidbody and don't know how to fix it. 1 Answer