Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gilgada · Mar 19, 2012 at 11:04 PM · rotationtransformcharactercontrollerfauxgravity

Problem with rotating a character that has faux gravity

Hi, I am using scripts from the following source: http://forum.unity3d.com/threads/8873-Faux-Gravity-making-my-brain-spin...-Help! This basically sets up a faux gravity system where objects will be pulled towards others as if they had their own central field of gravity. Very useful for achieving similar things to Super Mario Galaxy.

But anyway, my problem is that while I may have a moving character that can be moved forward, back, left and right with the arrow keys along my planet, I would love to be able to rotate the character using the left and right arrow keys, which in turn moves the camera attached to the player object. This would allow for better maneuvering of the character.

So far I have made some progress with it, the following code has been added to the project and calls the gravity script attached to the player, with if statements checking when a jump has occurred, so that the player cannot rotate in mid air. The dilemma is that because the player is moving around a planet, its rotation changes quite a lot; trying to change its rotation with simple script is ineffective as it still rotates it in respect to the game axes. My code is as follows:

using UnityEngine; using System.Collections;

public class PlayerTurn : MonoBehaviour { public float speed; public GameObject player; public PlayerGravityBody gravBody;

 public void Update()
 {
     Vector3 turnAngles = player.transform.rotation.eulerAngles;

     if(Input.GetKey ("left") && gravBody.doJump == false)
     {
                     Debug.Log(turnAngles.y);
         turnAngles.y -= speed;
     }
 if(Input.GetKey ("right") && gravBody.doJump == false)
     {
                     Debug.Log(turnAngles.y);
         turnAngles.y += speed;
     }
 }

}

this works okay until you start to pass the equator of the planet and then the rotations seem to reverse.

any help with this would be great :)

Comment
Add comment · Show 2
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 Dakwamine · Mar 20, 2012 at 12:00 AM 0
Share

I can't see where do you set the character rotation.

avatar image gilgada · Mar 20, 2012 at 12:02 AM 0
Share

the player gameObject is the character. the turnAngles Vector3 is declared as the player gameObject's rotation eulerAngles and is then edited in the if statements when both a key is pressed and the character is not jumping

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dakwamine · Mar 20, 2012 at 12:15 AM

I am not a C# developer, but it seems obvious that you are not setting the player rotation. You are just setting the variable turnAngles.

Besides that, could you try to use transform.Rotate() in Space.Self in order to rotate the character?

Comment
Add comment · Show 5 · 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
avatar image gilgada · Mar 20, 2012 at 10:21 AM 0
Share

I've tried transform.rotate() like you suggested but I'm not sure how to change the y rotation incrementally with this. The character just seems to spin frantically on the spot.

avatar image gilgada · Mar 20, 2012 at 10:28 AM 0
Share

Scratch that, I've appended the code again and it is getting closer to functioning as I would like. I have the following now:

using UnityEngine; using System.Collections;

public class PlayerTurn : $$anonymous$$onoBehaviour { public float speed; public GameObject player; public PlayerGravityBody gravBody; Vector3 turnAngles = new Vector3(0, 0, 0);

 public void Update()
 {


     if(Input.Get$$anonymous$$ey ("left") && gravBody.doJump == false)
     {
         Debug.Log(turnAngles.y);
         turnAngles.y -= speed;
         transform.Rotate(turnAngles, Space.Self);
     }
 if(Input.Get$$anonymous$$ey ("right") && gravBody.doJump == false)
     {
         Debug.Log(turnAngles.y);
         turnAngles.y += speed;
         transform.Rotate(turnAngles, Space.Self);
     }
 }

}

where speed is set to 0.1 in the inspector.

The problem is though, it still seems to rotate the character in strange ways. It does seem to turn it on its y-axis properly but at points on the planet, the left and right keys have varying effects.

avatar image gilgada · Mar 20, 2012 at 10:32 AM 0
Share

seem to have fixed it now, needed to change the second if statement to else if and then add an else statement below it to reset the y component of turnAngles

avatar image gilgada · Mar 20, 2012 at 10:33 AM 0
Share

thanks for your help :)

avatar image Dakwamine · Mar 21, 2012 at 06:02 PM 0
Share

Good job! :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do i rotate based on direction character is moving in 1 Answer

Always rotate the camera to be stood close to upright. 1 Answer

How to rotate an object without transform.Rotate 1 Answer

CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer

Rotating horizontally around a sphere 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