• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

183 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 avatar image avatar image avatar image

Related Questions

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

Movement with rigidbody 1 Answer

camera movement on a sphere 0 Answers

Jump function for this Method? 0 Answers

[I still need help even though there is 2 replies] Help with shooter script 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges