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 thatdudeinthecotton · Nov 22, 2018 at 12:11 PM · gravitytransform.rotationtransform.rotate

Trouble rotating a character to correctly match new gravity direction

Hey, I am making a game where the player changes the direction of gravity to jump across gaps and solve puzzles. Right now I'm making it so that the player looks in the given direction they want to switch gravity to, they hit space and they tilt 90 degrees, falling to where they want to go to until they hit a wall. The issue is that they aren't falling in the correct direction every time. Sometimes they'll fall right, left, or they won't fall at all. It's very unpredictable.

Here's my script. If anyone knows how to get the player to consistently fall in the direction they are facing that would be a great help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class GravityPlayer : MonoBehaviour
 {
     float Grav;
     float JumpForce;
     float JumpTime;
     public Rigidbody rb;
     public float smooth = 1f;
     private Quaternion targetRotation;
     int Jumpmanager;
 
 
     void Start()
     {
 
         Jumpmanager = 0;
         targetRotation = transform.rotation;
         JumpTime = 1f;
         Grav = -9.8f;
         JumpForce = 1f;
 
         rb = GetComponent<Rigidbody>();
         this.GetComponent<Rigidbody>().useGravity = false;
     }
 
     void FixedUpdate()
     {
 
         bool down = Input.GetKeyDown(KeyCode.Space);
 
         if (down)
         {
             
             if (Jumpmanager < 1)
             {
                 Jumpmanager = Jumpmanager + 1;
                 StartCoroutine(HandleIt());
             }
         }
 
     }
 
 
     private IEnumerator HandleIt()
     {
         Vector3 myForward = transform.TransformDirection(Vector3.forward);
         rb.velocity = new Vector3(0, 0, 0);
         gameObject.GetComponent<NavMeshAgent>().enabled = false;
         rb.isKinematic = false;
 
 
 
         rb.AddForce(transform.up * (JumpForce/2), ForceMode.Impulse);
 
         targetRotation *= Quaternion.AngleAxis(90, myForward);
     
     transform.rotation= Quaternion.Lerp(transform.rotation, targetRotation, 90 * smooth* Time.deltaTime);
 
 
 
         yield return new WaitForSeconds(1);
 
         rb.AddForce(transform.up * Grav, ForceMode.Impulse);
     }
 
 
     void OnCollisionEnter(Collision coll)
     {
         rb.isKinematic = true;
         gameObject.GetComponent<NavMeshAgent>().enabled = true;
         transform.rotation = Quaternion.identity;
         Jumpmanager = 0;
 
     }
     // use the syntax if(agent.isOnOffMeshLink){ } to trigger animations and other surface switching methods
 }
 


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

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

97 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

Related Questions

Issues with copying Y axisrotation of another object 2 Answers

Transform.eulerAngles only x and z 2 Answers

transform.Rotate vs. transform.rotation = when use which? 4 Answers

How to add limit to object rotation? 1 Answer

Transforming a single axis. 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