Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Tarook · Dec 23, 2021 at 09:03 PM · rotationmovementrigidbodygravityplanet

Why does the player not move around planet with gravity ?

Hello, I'm learning Unity and tried making a planet that has it's own gravity, I learned the gravity scripts by following a couple of tutorials and made the player movement by tweaking a bit a Third person playerController script I had learned from a Brackeys tutorial to use it on a rigidbody.

The player is a rigidbody and I use cinemachine to for the camera.

The gravity works fine, but I'm currently facing 2 problems that I can't solve.

First the script that manages the gravity and rotates the objects so that their bottom always faces the planet doesn't work on the player when he moves and when the player moves his rotation just goes back to 0,0,0.

Second the player doesn't move in front of him, he moves on the world's x and z axis(if I'm not wrong).

I've left some lines of the different things I've tried to fix this problem so that you may help me understand what I did wrong :

Player Movement (attached to player):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerMovement : MonoBehaviour
 {
     [SerializeField] private Rigidbody PlayerBody;
     [SerializeField] private Transform cam;
     [Space]
     [SerializeField] private Vector3 playerMovementInput;
     [SerializeField] private Vector3 direction;
     [Space]
     public float speed;
     public float jumpForce;
     public float turnSmoothTime = 0.1f;
     float smoothVelocity;
 
     void Update()
     {
         float horizontalInput = Input.GetAxisRaw("Horizontal");
         float verticalInput = Input.GetAxisRaw("Vertical");
         playerMovementInput = new Vector3(horizontalInput, 0f, verticalInput);
 
         direction = new Vector3(playerMovementInput.x, 0f, playerMovementInput.z).normalized;
     }
 
     private void FixedUpdate()
     {
         movePlayer();
     }
 
     private void movePlayer()
     {
         if (direction.magnitude >= 0.1f)
         {
             // Player rotation based on camera
             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg /*+ cam.eulerAngles.y*/;
             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref smoothVelocity, turnSmoothTime);
             transform.rotation = Quaternion.Euler(0f, angle, 0f);
 
             // Movement
             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
             PlayerBody.MovePosition(PlayerBody.position + transform.TransformDirection(direction) * speed * Time.deltaTime);
             //PlayerBody.velocity = new Vector3(moveDir.x, PlayerBody.velocity.y, moveDir.z) * Time.deltaTime;
 
             Debug.Log(targetAngle);
             Debug.Log(moveDir);
         }
     }
 }


Gravity (attached to player but should probably work on other objects)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class gravity : MonoBehaviour
 {
     Rigidbody rb;
     public float planetGrav;
     public Transform planetCenter;
     public GameObject currentPlanet;
     [Space]
     public bool AutoOrient;
     public float orientSpeed;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
         planetGrav = currentPlanet.GetComponent<planetSpecs>().gravity;
     }
 
 
     private void FixedUpdate()
     {
         applyGrav();
     }
 
     void applyGrav()
     {
         Vector3 diff = transform.position - planetCenter.position;
         rb.AddForce(-diff.normalized * planetGrav * (rb.mass));
 
         if (AutoOrient)
         {
             autoOrient(-diff);
         }    
     }
 
     void autoOrient(Vector3 dir)
     {
         Quaternion orientDir = Quaternion.FromToRotation(-transform.up, dir) * transform.rotation;
         transform.rotation = Quaternion.Slerp(transform.rotation, orientDir, orientSpeed * Time.deltaTime);
     }
 }

Planet information (so that I can make other planets at some point)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class planetSpecs : MonoBehaviour
 {
     public float gravity = 9.81f;
     public bool hasPlayer;
 }





Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tarook · Dec 24, 2021 at 01:21 AM

Update : So I've found out that the problems mentioned above were caused by the part of the script that was supposed to make the player rotate to the direction he is walking towards. All I need to do now is find a way to do that without breaking everything which I am unsure of since I rotate the transform on it's y angle based on the direction of the input and y angle of the camera which rotates with the player so I don't get why it rotates around the world's y angle.

Comment
Add comment · Share
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

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

298 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

Related Questions

After added a rigidbody in a GameObject, it moves on rotation 2 Answers

Problem with gravity, OnCollisionEnter and OnCollisionExit 2 Answers

Can't find a way to make object rotate and move at the same time. 1 Answer

Rotating Two Axes for Gravity,Rotating only two axes towards Object 0 Answers

Input AnyKey only working for one of my players? 0 Answers


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