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 /
  • Help Room /
avatar image
0
Question by Otaku_Riisu · Jun 23, 2016 at 04:16 AM · c#cameramovementmovement scriptrelative movement

Face direction of a Vector 3

Currently, I have a set up where the character I am controlling moves forward fine, but attempting to go any other direction causes problems.

Running forward (GIF)

Running not forward (GIF)

The goal here is to get the character to face the direction the joystick is pointed (it moves just fine, as you can see from the gifs)

Here are my scripts

Player motion

 using UnityEngine;
 using System.Collections;
 
 public class Motion : MonoBehaviour {
     Animator anim;
     Rigidbody rig;
     Camera cam;
     void Awake(){
     anim = GetComponent <Animator> ();
     rig = GetComponent<Rigidbody> ();
     cam = Camera.main;
     }
         
     void Update () {
         float horizontal = Input.GetAxis ("Horizontal"); 
         float vertical = Input.GetAxis ("Vertical");
         float joystickMagnitude = Mathf.Clamp01 (new Vector2 (horizontal, vertical).magnitude);
         float angle = Mathf.Atan2 (horizontal, vertical) * Mathf.Rad2Deg;
         anim.SetFloat ("Speed", joystickMagnitude);
 
         Vector3 movement = new Vector3 (0, Mathf.Atan2(horizontal, vertical), 0);
         rig.transform.Rotate(movement);
 
         if (Input.GetButtonDown ("Sprint")) {
             anim.Play ("sprinting_forward_roll");
         }
     }
 }

Camera Script

 using UnityEngine;
 using System.Collections;
 
 public class thirdPersonCamera : MonoBehaviour {
 
     public Transform lookAt;
     public Transform camTransform;
 
     private const float yAngleMin = -15.0f;
     private const float yAngleMax = 40.0f;
     private float distance = 2.0f;
     private float currentX = 0.0f;
     private float currentY = 0.0f;
     private float xSensitivity = 4.0f;
     private float ySensitivity = 2.0f;
 
     public void Start(){
         camTransform = transform;
     }
 
     public void Update(){
         currentX += Input.GetAxis("Mouse X") * xSensitivity;
         currentY += Input.GetAxis("Mouse Y") * ySensitivity;
         currentY = Mathf.Clamp (currentY, yAngleMin, yAngleMax);
     }
 
     public void LateUpdate(){
         Vector3 direction = new Vector3 (0, 0, -distance);
         Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
         camTransform.position = lookAt.position + rotation * direction;
         camTransform.LookAt (lookAt.position);
     }
 }

There is nothing wrong with the camera script. In fact, it is exactly how I want it for right now. What I would like to figure out is how to make the player face the direction the joystick is pointed, and if possible (although not necessarily) relative to the camera. I can probably figure out the camera relative part on my own once I figure out how to fix the motion script, although help is appreciated with that. What should I do? because clearly the solution of Mathf.Atan2(horizontal, vertical) is not working in this particular situation.

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 Otaku_Riisu · Jun 24, 2016 at 12:59 AM

I solved the issue on my own

 //Gets the direction in which the camera is facing
         Vector3 cameraDirection = cam.forward;
 //This Vector 3 represents a dead joystick, meaning it is not being pushed in any direction
         Vector3 deadJoystick = new Vector3(0,0,0);
 //The Vector 3 here gets the direction in which the joystick is being pushed on the controller
         Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);
 //keeps the camera's y value the same as that of the stickDirection for clean calculations
         cameraDirection.y = 0.0f;
 //Adjusts the direction of motion based on the position of the camera
         Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, cameraDirection);
 //Changes the angle of stickDirection to be camera-relative
         Vector3 moveDirection = referentialShift * stickDirection;
 //If the joystick is not being pressed, don't do anything
         if (moveDirection == deadJoystick) return;
 //rotate the player in the direction of the joystick
         rig.transform.rotation = Quaternion.LookRotation (moveDirection.normalized);

The rotation is slightly clunky, but it is easy enough to deal with for now. I hope this helped someone!

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

180 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

Related Questions

How to make an object go the direction it is facing? (Im new) 0 Answers

How to have a child object camera with gaze move its parent object 0 Answers

Camera is Moving in Inspector but not in game? 1 Answer

Moving relative to camera 0 Answers

Camera Modification Help 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