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 LyraxH · Oct 23, 2020 at 10:46 AM · transform.positiontransform.rotationhealthbar

How to make character move in the direction its facing

I'm trying to make my character move forwards in the way that the camera is facing, but it doesn't seem to work. I've tried multiple solutions in which none have worked. it still moves on the world space.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     public Transform Player;
     public Transform Head;
     public Rigidbody rb;
     public Camera camera;
     private float speed = 20.0f;
     private float jumpforce = 1.0f;
     public bool CanJump = true;
 
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         if (Input.GetKey(KeyCode.D))
         {
             transform.position += transform.right * speed * Time.deltaTime;
         }
 
         if (Input.GetKey(KeyCode.A))
         {
             transform.position -= transform.right * speed * Time.deltaTime;
         }
 
         if (Input.GetKey(KeyCode.Space))
         {
             if (CanJump == true)
             {
                 rb.velocity = new Vector3(0, 5, 0);
             }
         }
 
         if (Input.GetKey(KeyCode.W))
         {
             transform.position += transform.forward * Time.deltaTime * speed;
         }
 
         if (Input.GetKey(KeyCode.S))
         {
             transform.position -= transform.forward * speed * Time.deltaTime;
         }
     }
     
     void OnCollisionEnter(Collision collision)
     {
         CanJump = true;
     }
 
     void OnCollisionExit(Collision collision)
     {
         CanJump = false;
     }
 }
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
1
Best Answer

Answer by Klarzahs · Oct 23, 2020 at 11:07 PM

Hi @LyraxH,

I assume the script is attached to the player object? Then transform.forward (or any of the transform.X) is using the players transform. If you want to get the looking direction of the camera, use Camera.main.transform.forward.
Note: I think querying Camera.main was a bit performance heavy, so save it in a variable

Comment
Add comment · Show 3 · 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 LyraxH · Oct 23, 2020 at 11:39 PM 0
Share

Hi @Klarzahs

Thanks for the response, but I already have a script to make the body follow the camera position here: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Looking : $$anonymous$$onoBehaviour
 {
     public GameObject Head;
     float yaw = 0f;
     float pitch = 0f;
     public float lookspeed = 2.5f;
     public float maxY = -65;
     public float $$anonymous$$Y = 50;
     
     void Start()
     {
         Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
         Cursor.visible = false;
     }
 
     void Update()
     {
         transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
         yaw += lookspeed * Input.GetAxis("$$anonymous$$ouse X");
         pitch -= lookspeed * Input.GetAxis("$$anonymous$$ouse Y");
         pitch = $$anonymous$$athf.Clamp(pitch, maxY, $$anonymous$$Y);
     }
 }
 

but it still uses world space for some reason. Thanks

avatar image Klarzahs LyraxH · Oct 23, 2020 at 11:45 PM 1
Share

Are both of the scripts attached to the player? Then it should work, I copy/pasted it into a blank solution and it worked (even though your view rotation is method.. weird). If both are attached to the player, can you upload your $$anonymous$$imized project to github/dropbox etc, I'll have a look at it

avatar image LyraxH Klarzahs · Oct 23, 2020 at 11:48 PM 0
Share

Oh I got it to work, I just didn't have both scripts attached to the player. Thanks!

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

140 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

Related Questions

How to Move and Rotate Ball at the same time by code 0 Answers

any way to access the global position and rotation of a child object? 1 Answer

Vertical Camera Orbit 0 Answers

How to get updated Transform of a spawned object? 1 Answer

Possible way to improve performance moving large hierarchies? 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