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 highpockets · Jun 14, 2015 at 11:10 AM · player movementlookatcharacter movement

Player movement based on camera position and player orientation

Hi,

It has been a while since I have played around with unity and there may be a better way to do this, so feel free to say "that is a bad idea and you should do it like this..."

My character can have the transform.up positive and negative on every axis, so the number of possibilities for the player to navigate is 6 ( x/-x/y/-y/z/-z). The player is controlled perfectly while in the original orientation ( transform.up == y ), but when the player is parented to another object, then rotated, then un-parented, it doesn't work as expected.

The script is below, and it basically is supposed to take the vertical/horizontal axis based on the camera orientation and translate that to usable world directions.. Within the if statement of around line 50 is where things aren't adding up, I'm sure.

***EDIT: OOPs, I guess I should have mentioned what exactly was happening when "things weren't working as expected".. What happens, is the player ends up rotating back to transform.up == y when it should have transform.up == x.. So I believe the lookat functions are sending the wrong info, but I'm not quite sure where to start.

I'm going &)$(%^*#() nuts with this, so if anyone can help me, I would greatly appreciate it.. Thanks in advance.

 public class PlayerControl : MonoBehaviour
 {
     public delegate void EventHandler (GameObject e);
     public event EventHandler UpCubeRotate;
     public event EventHandler DownCubeRotate;
     public event EventHandler RightCubeRotate;
     public event EventHandler LeftCubeRotate;
 
     private Animator anim;
     private Vector3 upStateTop;
     private Vector3 upStateForward;
     private Vector3 upStateRight;
     public bool cubeRotWait = false;
 
     public Transform mainCam;
     public Vector3 cubeRotDir;
     public GameObject objectBelow;
     public GameObject cubeCenter;
 
     // Use this for initialization
     void Start ()
     {
         anim = GetComponent<Animator>();
         cubeCenter.transform.up = transform.up;
         upStateTop = transform.up;
         upStateForward = transform.forward;
         upStateRight = transform.right;
     }
 
     // Update is called once per frame
     void FixedUpdate ()
     {
         if( Input.GetKeyDown( KeyCode.Space ))
         {
             anim.SetFloat("Speed", 0);
             transform.parent = cubeCenter.transform;
             mainCam.transform.parent = cubeCenter.transform;
             cubeRotWait = true;
             Vector3 transPosUpOne = transform.position + (transform.up);
             RaycastHit hit;
 
             if (Physics.Raycast(transPosUpOne, -transform.up, out hit)) //change position to one of 4 points and get rid of physics raycasts below
             {
                 objectBelow = hit.transform.gameObject;
             }
         }
 
         if( Input.GetKey( KeyCode.UpArrow ) || Input.GetKey( KeyCode.DownArrow ) || Input.GetKey( KeyCode.RightArrow ) || Input.GetKey( KeyCode.LeftArrow ) )
         {
             if(!cubeRotWait)
             {
 
                 transform.parent = null;
                 float h = Input.GetAxis("Horizontal");                // setup h variable as our horizontal input axis
                 float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
                 
                 Vector3 moveVectorX = mainCam.forward * v;
                 Vector3 moveVectorY = mainCam.right * h;
                 
                 Vector3 moveVector = ( moveVectorX + moveVectorY ).normalized * Time.deltaTime;
 
                 print (moveVector.normalized);
 
                 if( ( transform.up == upStateTop ) || ( transform.up == -upStateTop ) )
                 {
                     cubeRotDir = new Vector3( moveVector.x, 0.0f, moveVector.z ).normalized;
                     transform.LookAt( transform.position + cubeRotDir );
                     print ("Top");
                 }
                 else if( ( transform.up == upStateForward ) || ( transform.up == -upStateForward ) )
                 {
                     cubeRotDir = new Vector3( moveVector.x, moveVector.y, 0.0f ).normalized;
                     transform.LookAt( transform.position + cubeRotDir );
                     print ("Forward");
                 }
                 else if( ( transform.up == upStateRight ) || ( transform.up == -upStateRight ) )
                 {
                     cubeRotDir = new Vector3( 0.0f, moveVector.y, moveVector.z ).normalized;
                     transform.LookAt( transform.position + cubeRotDir );
                     print ( "Right" );
                 }
 
                 print (cubeRotDir);
 
                 anim.SetFloat("Speed", 100);
             }
             else if( Input.GetKey( KeyCode.UpArrow ) && !(Input.GetKey( KeyCode.DownArrow )) && !(Input.GetKey( KeyCode.RightArrow )) && !(Input.GetKey( KeyCode.LeftArrow )) )
             {
                 if( UpCubeRotate != null )
                 {
                     UpCubeRotate( this.gameObject );
                 }
                 //cubeRotWait = false;
             }
             else if( Input.GetKey( KeyCode.DownArrow ) && !(Input.GetKey( KeyCode.UpArrow )) && !(Input.GetKey( KeyCode.RightArrow )) && !(Input.GetKey( KeyCode.LeftArrow )) )
             {
                 if( DownCubeRotate != null )
                 {
                     DownCubeRotate( this.gameObject );
                 }
                 //cubeRotWait = false;
             }
             else if( Input.GetKey( KeyCode.RightArrow ) && !(Input.GetKey( KeyCode.DownArrow )) && !(Input.GetKey( KeyCode.UpArrow )) && !(Input.GetKey( KeyCode.LeftArrow )) )
             {
                 if( RightCubeRotate != null )
                 {
                     RightCubeRotate( this.gameObject );
                 }
                 //cubeRotWait = false;
             }
             else if( Input.GetKey( KeyCode.LeftArrow ) && !(Input.GetKey( KeyCode.DownArrow )) && !(Input.GetKey( KeyCode.RightArrow )) && !(Input.GetKey( KeyCode.UpArrow )) )
             {
                 if( LeftCubeRotate != null )
                 {
                     LeftCubeRotate( this.gameObject );
                 }
                 //cubeRotWait = false;
             }
         }
         else
         {
             anim.SetFloat("Speed", 0);
         }
     }
 }
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
Best Answer

Answer by highpockets · Jun 14, 2015 at 04:16 PM

Never mind, I'm retarded!! All I needed to do is add the world up position in my LookAt functions:

 transform.LookAt( transform.position + cubeRotDir, centerCube.up ); //centerCube.up is always the up direction for my character, as it is the game object which parents my player for rotation..
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

21 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

Related Questions

3D RPG - Player movement - He automatically finds the fastest way and goes around everything. How to limit his movement? 1 Answer

Top Down Shooter - Look at Cursor with Movement Compensation 1 Answer

Lock Character to Z axis. 1 Answer

Why is this script not moving my character and constantly getting this error given below,why is this script not moving my character 2 Answers

how to make the player not rotate when i press left or right button ? 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