• 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
Question by ynm11 · Dec 24, 2022 at 11:34 AM · controllerflying

How to make Player rotate up and down based on Camera up and down?

I have the below code which moves Player in camera direction and also rotates Player left and right (around Y axis) based on camera rotation.

But how can I also get it to rotate Player up and down, based on Camera up and down? (by "up and down", I mean tilting forward and backward around the X axis)

For some reason all of my approaches are failing. Can anyone please let me know a possible working solution?

Reason: I'm trying to make the Controller a Flying Controller.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Planey : MonoBehaviour
 {
     public Rigidbody r;
     public CapsuleCollider capsule;
     public Transform cameraTransform;
  
     //Base Movement
     public float horizontalInput;
     public float verticalInput;
     public Vector3 moveDirection;   
     public int rotDegPerSecond = 720;
     public float speed;   
     public float clampXZValue;
 
     //GroundChecking
     public LayerMask groundLayerMask;
     public Transform groundCheckTransform;
     public bool isGrounded;
     public float groundCheckRadius = 0.25f;
     public float distanceToGround;
 
     
 
     void Awake()
     {
         r = GetComponent<Rigidbody>();
         capsule = GetComponent<CapsuleCollider>();
     }
 
     private void FixedUpdate()
     {
         GroundCheck();
         Move();
         StopSliding();        
     }
 
     void Move()
     {
         horizontalInput = Input.GetAxis("Horizontal");
         verticalInput = Input.GetAxis("Vertical");
 
         moveDirection = new Vector3(horizontalInput, 0.0f, verticalInput).normalized;
         moveDirection = (Quaternion.AngleAxis(cameraTransform.rotation.eulerAngles.y, Vector3.up) * moveDirection).normalized;
 
 
         if (moveDirection != Vector3.zero && moveDirection != Vector3.zero)
         {
             Quaternion targetRotation = Quaternion.LookRotation(moveDirection); //or moveDirectionRaw
             targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotDegPerSecond * Time.deltaTime);
             r.MoveRotation(targetRotation);
         }
 
         Vector3 force = moveDirection * speed;
         r.AddForce(force, ForceMode.VelocityChange);
 
         //Clamping
         Vector3 velocityH = new Vector3(r.velocity.x, 0, r.velocity.z);
         Vector3 velocityV = new Vector3(0, r.velocity.y, 0);
         r.velocity = Vector3.ClampMagnitude(velocityH, clampXZValue) + velocityV;
     }
  
 
 
     void StopSliding()
     {
         if (moveDirection == Vector3.zero)
         {
             Vector3 zeroMe = new Vector3(0, r.velocity.y, 0);
             r.velocity = zeroMe;
         }
     }
 
     public void GroundCheck()
     {
         isGrounded = Physics.CheckSphere(groundCheckTransform.position, groundCheckRadius, groundLayerMask);
         
         RaycastHit groundHit = new RaycastHit();
         if (Physics.Raycast(transform.position, -Vector3.up, out groundHit))
         {
             distanceToGround = groundHit.distance;
         }
     }
 }
Comment

People who like this

0 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 unity_I8FaTX-tZFc4lA · Dec 24, 2022 at 01:02 PM 0
Share

Is your camera rotating or not ? Because if it is not, then you should start with that.

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

158 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

Related Questions

Character Flying around uncontrollably 2 Answers

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

Third person controller 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

[script attached] Simple flying script 3 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