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 /
This question was closed Oct 12, 2016 at 01:00 AM by Armetron for the following reason:

Other

avatar image
0
Question by Armetron · Oct 07, 2016 at 02:37 AM · player movementtransformdirectionplayercamera

How do move a player on an xz plane with a top down orbit camera?

Hello world,

so in my game I have a 3rd person camera that is looking down upon my player and has the ability to orbit around him/her when holding down the right mouse button. (granted I cant change any of the offsets when in game, another problem to work on)

 public class cameraController : MonoBehaviour {
 
     public float turnSpeed = 4.0f;
     public Transform player;
     public float zOffset = 7.0f;
     public float yOffset = 8.0f;
 
     private Vector3 offset;
 
     void Start() {
 
         offset = new Vector3(player.position.x, player.position.y + yOffset, player.position.z + zOffset);
         transform.position = player.position + offset;
     }
 
     void LateUpdate () {
         if(Input.GetMouseButton(1)) {
             offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
         }
 
         transform.position = player.position + offset;
         transform.LookAt(player.position);
     }
 }
 

Using this camera controller I want to be able to move the player using the WASD keys depending on the camera. This is what I got.

 public class playerController : MonoBehaviour {
 
     public float moveSpeed;
 
     private Rigidbody rb;
     private Camera playerCamera;
     private Vector3 keyboardInput;
     private Vector3 playerDirection;
     private Vector3 moveVelocity;
     private int cameraLayermask;
 
     // Use this for initialization //
     void Start() {
 
         rb = GetComponent<Rigidbody>();
         playerCamera = FindObjectOfType<Camera>();
 
         // Layers for Camera Raycast to focus on //
         int groundLayer = 8;
 
         // Add layers to Layermask
         cameraLayermask = 1 << groundLayer;
     }
 
     // Update is called once per frame //
     void Update() {
 
         keyboardInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
         playerDirection = playerCamera.transform.TransformDirection(keyboardInput);
         playerDirection.y = 0f;
         print(playerDirection);
         moveVelocity = playerDirection * moveSpeed;
 
         if (!Input.GetMouseButton(1)) {
 
             Ray cameraRay = playerCamera.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(cameraRay, out hit, Mathf.Infinity, cameraLayermask)) {
 
                 Debug.DrawLine(cameraRay.origin, hit.point, Color.blue);
 
                 transform.LookAt(new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z));
             }
         }
     }
 
     void FixedUpdate() {
 
         //Replace Gravity
         moveVelocity.y = rb.velocity.y;
         rb.velocity = moveVelocity;
 
     }
 }

Since I'm using

playerCamera.transform.TransformDirection(keyboardInput)

the cube is going to want to get pushed into the ground, in order to prevent this I added

playerDirection.y = 0f

this however has lead to a very slow moving forward motion.

What can I do to fix this?

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 Armetron · Oct 07, 2016 at 02:53 AM 0
Share

I just used vector3.Normalize and while it "technically" works the movement was not as smooth as as when without it. Hopefully there is a better solution.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

75 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

Related Questions

Can someone help me explain this Script here :) ? Help would be greatly appreciated 0 Answers

Return First person Rig to Start 0 Answers

falling script flaws 0 Answers

Help!--How to make player follow finger touch position in Space Shooter? 0 Answers

How to jump between -1 ,0 ,1 on x axis, using A and D keys? 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