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 Loki_illusions · Sep 30, 2016 at 02:30 PM · transformmovingfirst person controller

Move a player in the direction he is facing.

I have converted the survival shooter into a first person shooter, but am having difficulties getting him to go in the direction the camera is facing instead of the pos/neg x and z axes.

Here is my code, anyone have a good way to do this? I was thinking it should be something using translate instead of Vector3, but wasn't totally sure how to do that without messing everything up.

using UnityEngine; public class PlayerMovement : MonoBehaviour { %|1837095373_1|% %|-1988135342_2|% Animator anim; %|573970496_4|% %|1009415456_5|% %|-1717068021_6|% %|-416879092_7|% %|-1063181321_8|% %|-347868518_9|% %|309713818_10|% %|-1476747494_11|% %|-1764302161_12|% %|752330654_13|% %|855801838_14|% %|-20968000_15|% %|1984206577_16|% %|-824875282_17|% float v = Input.GetAxisRaw ("Vertical"); %|248628461_19|% Turning (); %|1311444592_21|% %|-244052613_22|% void Move (float h, float v) %|2038457363_24|% %|-1866289867_25|% %|573705482_26|% %|44033565_27|% %|1202127834_28|% %|-198277706_29|% %|56915890_30|% %|1040459942_31|% %|1437352416_32|% %|463237216_33|% %|-1818838061_34|% %|72649422_35|% %|1364433722_36|% %|960389714_37|% %|1837724170_38|% %|-1587850171_39|% %|1586321773_40|% %|-34726812_41|% Quaternion newRotation = Quaternion.LookRotation (playerToMouse); %|-742224437_43|% %|-1571266492_44|% } %|83865066_46|% %|1855510554_47|% { %|151747258_49|% %|1140548255_50|% %|1242713347_51|% } Thanks!

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 Loki_illusions · Sep 30, 2016 at 02:32 PM 0
Share
 using UnityEngine;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
     public float speed = 6f; //How fast he moves//
     Vector3 movement;
     Animator anim;
     Rigidbody playerRigidbody;
     int floor$$anonymous$$ask;
     float camRayLength= 100f;
 
 
     public Transform from;
     public float sensitivity = 0.3f;
 
     void Awake ()
     {
         floor$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask ("Floor");
         anim = GetComponent<Animator> ();
         playerRigidbody = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate ()
     {
         float h = Input.GetAxisRaw ("Horizontal");
         float v = Input.GetAxisRaw ("Vertical");
 
         $$anonymous$$ove (h, v);
         Turning ();
         Animating (h, v);
 
 
     }
 
     void $$anonymous$$ove (float h, float v)
     {
         movement.Set (h, 0f, v);
         movement = movement.normalized * speed * Time.deltaTime;
         playerRigidbody.$$anonymous$$ovePosition (transform.position + movement);
 
         if (Input.Get$$anonymous$$eyDown ("space")&& playerRigidbody.transform.position.y <= 0.8f)        //When you press space the player jumps, as long as he is on the ground//
             {
                 Vector3 jump = new Vector3 (0.0f, 200.0f, 0.0f);
                 playerRigidbody.AddForce (jump);
             }
     }
 
     void Turning ()
     {
         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
 
         RaycastHit floorHit;
 
         if (Physics.Raycast (camRay, out floorHit, camRayLength, floor$$anonymous$$ask)) 
         {
             Vector3 playerTo$$anonymous$$ouse = floorHit.point - transform.position;
             playerTo$$anonymous$$ouse.y = 0f;
 
             Quaternion newRotation = Quaternion.LookRotation (playerTo$$anonymous$$ouse);
             playerRigidbody.$$anonymous$$oveRotation (newRotation);
             transform.localRotation = Quaternion.Slerp(from.rotation, newRotation, Time.deltaTime* sensitivity);
         }
     }
 
     void Animating (float h, float v)
     {
         bool walking = h != 0f || v!=0f;
         anim.SetBool ("IsWalking", walking);
     }
 }
 

Sorry, the code above got weird formating for some reason

1 Reply

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

Answer by conman79 · Sep 30, 2016 at 03:46 PM

You need to move your object relative to the rigidbody's local rotation with the help of Transform.forward and Transform.right.

Change your Move function to look like this and your character will move relative to it's local rotation.

 void Move (float h, float v)
     {
         movement.Set (h, 0f, v);
         movement = movement.normalized * speed * Time.deltaTime;
 
         playerRigidbody.MovePosition (transform.position + (transform.forward * movement.z) + (transform.right * movement.x));
 
         if (Input.GetKeyDown ("space")&& playerRigidbody.transform.position.y <= 0.8f)        //When you press space the player jumps, as long as he is on the ground//
         {
             Vector3 jump = new Vector3 (0.0f, 200.0f, 0.0f);
             playerRigidbody.AddForce (jump);
         }
     }

Comment
Add comment · Show 1 · 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 Loki_illusions · Sep 30, 2016 at 04:05 PM 0
Share

Thanks, that worked perfectly!

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

84 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

Related Questions

How do i move a cube by one of its Vertices/Vertex 1 Answer

Transform.Rotate x and y rotate z, where z is 0 0 Answers

how can i know if this transform is moving ??? 1 Answer

my bullets are flaoting upwards and i dont know why? 1 Answer

Lerp Random Position - Within Original Vector3 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