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 Karbonic · Aug 06, 2020 at 01:07 AM · cameraplayercamera-movementplayer movement

Moving Player Relative to Camera?

How would be the most efficient way to have the player move relative to the camera's rotation? Here's what I have right now. Currently, my script uses Vector3, which I know works off of worldspace rather than local space, is there any way to continue using Vector3, or will I have to restart the script from the ground up?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerMovement : MonoBehaviour
 {
     public Rigidbody rigidBody;
     public float moveSpeed, jumpForce;
 
     private Vector2 moveInput;
     private float horizontalMove;
     private float verticalMove;
     public float horizontalTurn;
 
     public LayerMask ground;
     public Transform footPoint;
     public bool grounded;
     public float inputTest;
     public Animator anim;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         //movement
         //moveInput.x = Input.GetAxis("Horizontal");
         //moveInput.y = Input.GetAxis("Vertical");
         horizontalMove = Input.GetAxis("Horizontal");
         verticalMove = Input.GetAxis("Vertical");
 
         moveInput.Normalize();
 
         inputTest = Input.GetAxis("Horizontal");
 
         rigidBody.velocity = new Vector3(horizontalMove * moveSpeed, rigidBody.velocity.y, verticalMove * moveSpeed);
 
         //Sprint
         if(Input.GetButtonDown("Fire1") && grounded)
         {
             moveSpeed = 4;
         }
         else
         {
             moveSpeed = 2;
         }
             
         //animator control
         if ((rigidBody.velocity.z != 0)||(rigidBody.velocity.x !=0))
         {
         anim.SetFloat("Vertical",rigidBody.velocity.z);
         anim.SetFloat("Horizontal",rigidBody.velocity.x);
         }
 
         anim.SetFloat("Speed", rigidBody.velocity.magnitude);
 
         anim.SetBool("Grounded",grounded);
 
         //Check for ground contact and jump
         RaycastHit hit;
         if(Physics.Raycast(footPoint.position, Vector3.down, out hit, .3f, ground))
         {
             grounded = true;
         } else
         {
             grounded = false;
         }
 
         if(Input.GetButtonDown("Jump") && grounded)
         {
             rigidBody.velocity += new Vector3(0f, jumpForce, 0f);
         }
     }
 }
 


You'll have to forgive the messy code, this is a first draft.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Aug 06, 2020 at 07:40 AM

         bean.transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0); //Locks the y rotation of the player to the camera's y rotation
         transform.position = new Vector3 (bean.transform.position.x, bean.transform.position.y + 0.5f, bean.transform.position.z); //Moves the camera with the player

Well im still kinda confused about what youre trying to do. Im guessing that you want the player's rotation to match the camera's rotation? Or the player's position match the camera's position? Anyway, heres both. bean.transform is the transform of my player, and this code is attached to the main camera. It doesnt have to be but instead of writing transform.position etc. you could write camera.transform.position. Hope it helps

Comment
Add comment · 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

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

232 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 problem 2 Answers

Camera and Player position 0 Answers

Player's movements doesn't match orbital camera 1 Answer

Don'tDetroyOnLoad | issue 1 Answer

Make a ridgid body move in the direction the camera is facing 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