Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
1
Question by andycodes · Feb 10, 2016 at 08:30 PM · movementinputaxisoffsetinput.getaxis

change character movement axis in respect to camera orientation?

Hi all, so a few days ago i did the basic "Unity roll-a-ball" tutorial and spent some time afterwards playing around with the character controller. so far, i have an empty player object 'player' that has a few children; 'Ball' object with a rigidbody and a PlayerController script to move it around that looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
 
     private Rigidbody rb;
 
     void Start () {
         rb = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate () {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }
 }
 

and 'Gyro' empty object with the main camera attached. the 'gyro' has a script that originally only kept the gyro object offset from the rotation of the Ball so that i could just attach the camera to it. i extended on its abilities a bit by adding in a chunk of code to allow the user to orbit the ball based on moving the mouse. the gyroScript looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class gyroScript : MonoBehaviour {
 
     public GameObject target;
     // the target in this case is the ball object
     public float speed = 10f;
     public float rotationSpeed = 100.0f;
     private Vector3 offset;
 
     void Start () {
         offset = transform.position - target.transform.position;
     }
 
     void Update()
     {
         mouseOrbit();
     }
 
     void LateUpdate () {
         transform.position = target.transform.position + offset;
             // this keeps the gyro object stable in comparison to the ball object
     }
 
     void mouseOrbit()
     {
         float translation = Input.GetAxis("Mouse Y") * speed;
         float rotation = Input.GetAxis("Mouse X") * rotationSpeed;
         translation *= Time.deltaTime;
         rotation *= Time.deltaTime;
         transform.Translate(0, 0, translation);
         transform.Rotate(0, rotation, 0);
             // this rotates the gyro object about the Y axis by means of mouse movement
             // because the camera is a child of this it rotates about with it
     }
 }


the code here does not rotate the camera but the gyro object which results in the camera being rotated about the object because it is parented.

My Question:

when i orbit the gyro, the player controller script does not adjust (e.i. to set the W key as the 'forward', D to right, ect...) based on the orientation of the gyro object because it is basing what is forward on the global horizontal and vertical as defined in the Player Controller Script.

how would i go about linking the two together so when i turn the gyro object and press to go 'forward', i go forward based on the direction the gyro object is facing rather than the global horizontal and vertical axis?

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
3
Best Answer

Answer by andycodes · Feb 10, 2016 at 11:10 PM

I discovered the solution!

It is required to set a target to the gyro object and delete the old vector3 and modify the add force attributes as such:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
 
     private Rigidbody rb;
 
     public GameObject target; //new code
     // target is the object you will take the rotations from
 
 
     void Start () {
         rb = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate () {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
 
         //Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); (old code)


         rb.AddForce(moveVertical * target.transform.forward * speed); //new code
         rb.AddForce(moveHorizontal * target.transform.right * speed); //new code


         //rb.AddForce (movement * speed); (old code)
 
     }
 }
Comment
Add comment · Show 1 · 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 Gemesil · Jul 16, 2020 at 01:45 PM 0
Share

This works but only partly, the camera seems to teleport around every few seconds or so and if you hold both keys from a different axis like W and D or A and S it glitches out.

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

42 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

Related Questions

How Do I Snap Input Between 1, 0, and -1 0 Answers

Simulate keyboard buttons to use Input.GetAxis ("Horizontal") 2 Answers

'GetAxis' is not a member of 'function(): void' 1 Answer

Input Axis is Not Setup 1 Answer

Controller Joystick Hold Delay Logic? 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