Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sylon87 · May 01, 2019 at 05:06 AM · c#rigidbodywalkingwalkphisics

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;

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KevRev · May 01, 2019 at 07:35 AM 0
Share

I did a quick search on here and the first result has a good answer.

Walk on sphere or cube

avatar image Sylon87 KevRev · May 01, 2019 at 08:24 AM 0
Share

it doesn't work on Unity 5+ but thank's you for your answer

avatar image KevRev Sylon87 · May 01, 2019 at 10:09 AM 0
Share

Oh sorry. This other post has a larger thread and various solutions and explanations. This would be a good place to start:

Another option

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

619 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges