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 Bigbossbro · Jan 16, 2017 at 10:50 AM · camera-movementcamera rotatecamera-lookcamera follow3rd person controller

I want to make rotation on XZ axis. But it rotates at XYZ every axis. Can anybody tell me how can I do XZ rotation?

This is a code that is attached to camera.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCamera : MonoBehaviour
 {
     public Transform Target;
     //public float Distance = 5.0f;
     public float xSpeed = 250.0f;
     public float ySpeed = 120.0f;
     public float yMinLimit = -20.0f;
     public float yMaxLimit = 80.0f;
     private float x;
     private float y;
     public float CameraXOffset = 0.2f;
     public float CameraYOffset = 0.0f;
     public float CameraZOffset = -1.21f;
 
     void Start()
     {
     }
 
     void Awake()
     {
         Vector3 angles = transform.eulerAngles;
         x = angles.x;
         y = angles.y;
     }
 
     void LateUpdate()
     {
         if (Target != null)
         {
             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             float Ax;
             Ax = x;
             float Ay;
             Ay = y ;
             Quaternion rotation = Quaternion.Euler(y, x, 0);
             Quaternion AnotherRotation = Quaternion.Euler(Ay, 0, 0);
             Vector3 position = rotation * (new Vector3(CameraXOffset, CameraYOffset, CameraZOffset)) + Target.position;
             Vector3 AnotherPosition = AnotherRotation * (new Vector3(CameraXOffset, CameraYOffset, CameraZOffset)) + Target.position;
             transform.rotation = AnotherRotation;
             transform.position = AnotherPosition;
         }
     }
 
     private float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360)
         {
             angle += 360;
         }
         if (angle > 360)
         {
             angle -= 360;
         }
         return Mathf.Clamp(angle, min, max);
     }
 }

And there is other code that is attached to Player.

 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 using System.Linq;
 using System;
 
 public class PlayerController : MonoBehaviour
 {
     [SerializeField] Animator Anim;
     [SerializeField] Camera m_Cam;
     
     private Vector3 moveDirection = Vector3.zero;
     private bool IsGrounded = false;
     private float movementSpeed = 5.0f;
     public bool CanPlayerMove = true;
     private float HorizontalAxis;
     private float VerticalAxis;
     private float DirectionDampTime = .25f;
     private bool IsRunning = false;
 
     public Transform PlayerTransform;
     public Transform PlayerHeadTransform;
     
     public float turnSpeed = 4.0f;
     private Vector3 CameraOrigin;
     private Vector3 PlayerOrigin;
     private Vector3 VectorBetweenCameraOriginAndPlayerOrigin;
     // private Vector3 offsetY;
     private bool CameraCanMove = true;
 
     private float MouseXAxis;
     private float MouseYAxis;
     private Vector3 MouseAxisVector;
     private Vector3 VerticalAxisAndHorizontalAxis;
     public float VerticalAxisAndHorizontalAxisMagnitude;
 
     private float MouseYAxisMinLimit = -20.0f;
     private float MouseYAxisMaxLimit = 80.0f;
     private Vector3 CameraDistance;
     private Quaternion CameraRotation;
 
     public int Sanfs { get; private set; }
 
     private void Start()
     {
         //offsetX = new Vector3(PlayerHeadTransform.position.x, PlayerHeadTransform.position.y + 8.0f, PlayerHeadTransform.position.z + 7.0f);
     }
 
     private void Update()
     {
         if(CanPlayerMove == true)
         {
             PlayerMovement();
         }
     }
     
 
     private void LateUpdate()
     {
         if (CameraCanMove == true)
         {
         }
         
     }
 
     void PlayerMovement()
     {
         VerticalAxis = Input.GetAxis("Vertical") * 2;
         HorizontalAxis = Input.GetAxis("Horizontal") * 2;
         VerticalAxisAndHorizontalAxis = new Vector3(HorizontalAxis, 0f, VerticalAxis);
         VerticalAxisAndHorizontalAxisMagnitude = VerticalAxisAndHorizontalAxis.magnitude;
         //VerticalAxisAndHorizontalAxis = m_Cam.transform.position;
         VerticalAxisAndHorizontalAxis = m_Cam.transform.TransformVector(VerticalAxisAndHorizontalAxis);//m_Cam.transform.TransformDirection(VerticalAxisAndHorizontalAxis);
         
         Anim.SetFloat("Speed", VerticalAxisAndHorizontalAxisMagnitude);
         CheckIdle();
         if (VerticalAxisAndHorizontalAxisMagnitude > 0)
         {
             CheckWalk();
             if (Input.GetKey(KeyCode.LeftShift))
             {
                 CheckRun();
             }
         }
     }
 
     public void CheckRun()
     {
         Anim.SetBool("IsRunning", true);
         Anim.SetBool("IsSprinting", false);
         Anim.SetBool("IsWalking", false);
         Anim.SetBool("IsIdle", false);
     }
 
 
     
     public void CheckWalk()
     {
         //PlayerTransform.rotation = new Quaternion(PlayerTransform.rotation.x, m_Cam.transform.rotation.y, PlayerTransform.rotation.z, PlayerTransform.rotation.w);
         PlayerTransform.rotation = Quaternion.LookRotation(VerticalAxisAndHorizontalAxis.normalized);
         
         // float PlayerRotantionAngle = m_Cam.transform.eulerAngles.y;
         // PlayerTransform.position = Vector3.Scale(m_Cam.transform.TransformVector(InputMove), new Vector3(1f, 0f, 1f)).normalized;
         // PlayerTransform.transform.rotation = InputMoveQuarternion;
         
         Anim.SetBool("IsIdle", false);
         Anim.SetBool("IsSprinting", false);
         Anim.SetBool("IsRunning", false);
         Anim.SetBool("IsWalking", true);
     }
 
     public void CheckSprint()
     {
         Anim.SetBool("IsIdle", false);
         Anim.SetBool("IsSprinting", true);
         Anim.SetBool("IsRunning", false);
         Anim.SetBool("IsWalking", false);
     }
 
     public void CheckIdle()
     {
         //CheckButton();
         Anim.SetBool("IsIdle", true);
         Anim.SetBool("IsSprinting", false);
         Anim.SetBool("IsRunning", false);
         Anim.SetBool("IsWalking", false);
     }
 }

Anybody can kindly help me? I'm using unity 5.4.3f1.

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

0 Replies

· Add your reply
  • Sort: 

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

89 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

Related Questions

Spyro Like Camera Follow 0 Answers

Top-Down Camera view (with rotation) need help (third person script) 0 Answers

getting Jittery movement on camera when player rotating and moving in same time 0 Answers

CineMachine Camera Movement is inverse 1 Answer

C# Smooth Follow Space Ship 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