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 /
This question was closed Oct 17, 2015 at 08:35 AM by Karmate for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Karmate · Oct 16, 2015 at 11:42 AM · character movementcamera movement

How to move and rotate character according to camera?

I stuck on movement system according to camera. When i move from back of character, everything is ok. But when i look from top of character (top-down), character don't move (trying to go down). Anyone help please. And how can i add character look to input (with lerp) in this example.

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class Player : MonoBehaviour
 {
     public TPCamera tpCamera;
     Vector2 input;
     Rigidbody rigid;
     public float moveSpeed;
     float speed, direction;
     Quaternion _rotation;
 
     void Start ()
     {
         rigid = GetComponent<Rigidbody> ();
     }
 
     void LateUpdate ()
     {
         CameraInput ();
     }
 
     void CameraInput ()
     {
         #if MOBILE_INPUT
         tpCamera.mouseX += CrossPlatformInputManager.GetAxis ("Mouse X") * tpCamera.X_MouseSensitivity;
         tpCamera.mouseY -= CrossPlatformInputManager.GetAxis ("Mouse Y") * tpCamera.Y_MouseSensitivity;
         #else
         tpCamera.mouseX += Input.GetAxis ("Mouse X") * tpCamera.X_MouseSensitivity;
         tpCamera.mouseY -= Input.GetAxis ("Mouse Y") * tpCamera.Y_MouseSensitivity;
         #endif
         tpCamera.mouseY = Helper.ClampAngle (tpCamera.mouseY, tpCamera.Y_MinLimit, tpCamera.Y_MaxLimit);
         tpCamera.mouseX = Helper.ClampAngle (tpCamera.mouseX, tpCamera.X_MinLimit, tpCamera.X_MaxLimit);
     }
 
     void FixedUpdate ()
     {
         ControllerInput ();
         ControlLocomotion ();
     }
 
     void ControllerInput ()
     {
         #if MOBILE_INPUT
         input = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical"));    
         #else
         input = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
         #endif
         input.Normalize ();
     }
 
     void ControlLocomotion ()
     {
         //Not sure is this a good solution to rotate ???
     if (input != Vector2.zero)
     {
         _rotation = Quaternion.LookRotation(targetDirection, Vector3.up);

         Vector3 lookDirection = targetDirection.normalized;
         _rotation = Quaternion.LookRotation(lookDirection);
         rigid.rotation = Quaternion.Slerp(transform.rotation, _rotation, rotationSpeed);
     }

     //Move Character with rigidbody according to camera ignoring y
     }
 
     Vector3 targetDirection {
         get {
             Vector3 cameraForward = tpCamera.transform.TransformDirection (Vector3.forward);
             cameraForward.y = 0;    //set to 0 because of camera rotation on the X axis
             
             //get the right-facing direction of the camera
             Vector3 cameraRight = tpCamera.transform.TransformDirection (Vector3.right);
             
             // determine the direction the player will face based on input and the camera's right and forward directions
             Vector3 refDir = input.x * cameraRight + input.y * cameraForward;
             return refDir;
         }
     }
 }



Comment
Add comment · Show 8
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 graciasluigi · Oct 16, 2015 at 01:09 PM 0
Share

Dont you need to rotate/move the character based on the camera up direction ins$$anonymous$$d of the forward(talking about top down mode)?

avatar image Karmate graciasluigi · Oct 16, 2015 at 01:40 PM 0
Share

Character must always move and rotate. Camera is controlled by mouse. When I look from top, character doesn't move. (i am not good at english, sorry i dont get your question) Edit: i removed rigid.velocity from script.

avatar image graciasluigi Karmate · Oct 16, 2015 at 01:50 PM 0
Share

So, when your camera is looking from top, try using the camera up direction ins$$anonymous$$d of forward. Does this test work?

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character and Camera motion smooth in Editor, but jitters in Build 1 Answer

How to prevent camera jitter when moving my Rigidbody first person controller 4 Answers

Moving character towards direction fast 2 Answers

Running wind effect 1 Answer

How to fix AI moving with my Input instead of self navigation 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