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 Diegus · Sep 21, 2013 at 12:07 PM · cameraplayerquaterniondirectionfollow

How to don't face player to camera direction?

Hello!

I'm using that script to rotate my player in a 2.5D game:

 using UnityEngine;
 using System.Collections;
  
 public class MoveLook : MonoBehaviour {
  
     public float lookSpeed = 10;
     private Vector3 curLoc;
     private Vector3 prevLoc;
  
     void Update () 
     {
        InputListen();
        transform.rotation = Quaternion.Lerp (transform.rotation,  Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
     }
  
     private void InputListen()
     {
        prevLoc = curLoc;
        curLoc = transform.position;
  
        if(Input.GetKey(KeyCode.A))
          curLoc.x -= 1 * Time.fixedDeltaTime;
        if(Input.GetKey(KeyCode.D))
          curLoc.x += 1 * Time.fixedDeltaTime;
        if(Input.GetKey(KeyCode.W))
          curLoc.z += 1 * Time.fixedDeltaTime;
        if(Input.GetKey(KeyCode.S))
          curLoc.z -= 1 * Time.fixedDeltaTime;
  
        transform.position = curLoc;
  
     }
 }


It works perfectly but when the player is in idle state it faces to Z axis, not to the current position.

My game is like a 2D Game but you can move to Z axis too. I have an static camera that's only moves to x and y axis but alway is looking to Z axis.

Im trying to solve this but im stuck.

Comment
Add comment · Show 1
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 Diegus · Sep 22, 2013 at 10:33 AM 0
Share

Ok. That's the correct script for EasyJoystick, works perfectly.

 using UnityEngine;
 using System.Collections;
  
 public class $$anonymous$$oveLook : $$anonymous$$onoBehaviour {
  
     public float lookSpeed = 10;
     public EasyJoystick joystick;
     private Vector3 curLoc;
     private Vector3 prevLoc;
  
     void Update () 
     {
         if(joystick.JoystickAxis.x > 0.1 || joystick.JoystickAxis.y > 0.1 || joystick.JoystickAxis.x < -0.1 || joystick.JoystickAxis.y < -0.1 ){
                    InputListen();
                    transform.rotation = Quaternion.Lerp (transform.rotation,  Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
             }
     }
  
     private void InputListen()
     {
        prevLoc = curLoc;
        curLoc = transform.position;
  
        if(joystick.JoystickAxis.x < 0)
          curLoc.x -= 1 * Time.fixedDeltaTime;
        if(joystick.JoystickAxis.x > 0)
          curLoc.x += 1 * Time.fixedDeltaTime;
        if(joystick.JoystickAxis.y > 0)
          curLoc.z += 1 * Time.fixedDeltaTime;
        if(joystick.JoystickAxis.y < 0)
          curLoc.z -= 1 * Time.fixedDeltaTime;
  
        transform.position = curLoc;
  
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Sep 21, 2013 at 02:11 PM

If I understand what you are asking you want the last rotation left alone if the player is not moving.

 void Update () {
     if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S)) {
        InputListen();
        transform.rotation = Quaternion.Lerp (transform.rotation,  Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
     }
 }
Comment
Add comment · Show 3 · 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 Diegus · Sep 21, 2013 at 02:25 PM 0
Share

That's work.

Now i'm trying to modify that script to a Joystick input like EasyJoystick. I think the easy way it's changing the "if(Input.Get$$anonymous$$e..." for my own joysticks axis inputs, right?

avatar image robertbu · Sep 21, 2013 at 03:25 PM 0
Share

You'll have two axes, one for horizontal and one for vertical. This means that you code will allow your character to rotate arbitrarily rather than just the four directions.

avatar image Diegus · Sep 30, 2013 at 08:29 PM 0
Share

Yep, my character rotates well when input Axis its 1 or -1, if X and Y axis are active at same time it rotate arbitrarily.

How to fix that?

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

15 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

Related Questions

Camera not rotate when following player 1 Answer

How to get camera to follow player 2d 11 Answers

Why does the camera smoothly follow in one direction but not the other? 0 Answers

Player look in camera direction problem 1 Answer

How can I respawn my RigidBodyFPSController facing new direction? 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