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 Jun 18, 2016 at 05:11 AM by Gurekam-Dhillon.
avatar image
1
Question by Gurekam-Dhillon · Apr 02, 2016 at 05:25 AM · c#3dtop down shooter

Moving the character LEFT and RIGHT relative to its rotation

Hi, so as the title says I want to add left and right movement to my character relative to its rotation. I've done this to forwards and backwards, but left and right isn't as simple.

Code :

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
     public float speed;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.W)) {
             transform.position += transform.forward * (float)Time.deltaTime * speed;
         }
         if (Input.GetKey (KeyCode.S)) {
             transform.position -= transform.forward * (float)Time.deltaTime * speed;
         }
     }
 }
 

Thats its, quite simple. So once again, I want to add right and left movement relative to the rotation of the player.

Comment
Add comment · Show 2
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 Gurekam-Dhillon · Apr 02, 2016 at 04:01 AM 0
Share

Also, this is for a top down shooter! this may or may not clear some things up.

avatar image Rajeesh_AR · Apr 02, 2016 at 08:53 AM 2
Share

Hi @Peace , I got this answer from Unity answers itself. You may try this..

 //$$anonymous$$ovement
 //$$anonymous$$ove forwards and backwards
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
     moveDirection.x -= 1;
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
     moveDirection.x += 1;
 //$$anonymous$$ove up and down
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
     moveDirection.y += .5f;
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
     moveDirection.y -= .5f;
 //Turn left and right
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
     gameObject.transform.Rotate(0, 90 * Time.deltaTime, 0);
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
     gameObject.transform.Rotate(0, -90 * Time.deltaTime, 0);
 
 //Update position
 transform.position += moveDirection.x * transform.right;
 transform.position += moveDirection.y * transform.up;



Hope this will help you..

2 Replies

  • Sort: 
avatar image
3

Answer by miszelfox · Apr 02, 2016 at 06:00 PM

Instead of changing transform.position you can try using transform.translate.

 if (Input.GetKey (KeyCode.A)) {
 transform.Translate(Vector3.left * Time.deltaTime * speed, Space.Self); //LEFT
 }
 if (Input.GetKey (KeyCode.D)) {
 transform.Translate(Vector3.right * Time.deltaTime * speed, Space.Self); //RIGHT
 }

Space.Self makes it relative to local rotation. Make the same with forward and backward by using Vector3.Forward and Vector3.Back

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 MC56 · Jun 17, 2020 at 09:27 PM 0
Share

Hi thanks for sharing this. It has saved me hours of research and trial and error! BTW, I'm new to game making :)

avatar image
0

Answer by Kureiji · Apr 02, 2016 at 11:37 AM

Hi !

I hope this answer your question :

 public float speed = 1f;
     
     void FixedUpdate () {
         // 1. Get the Input information
         // You can custom the axis key and sensibility at
         // Edit > Project Settings > Input
         float h = Input.GetAxis ("Horizontal");
         float v = Input.GetAxis ("Vertical");
     
         // 2. Create the move vector
         // /!\ Input Horizontal axis positive button
         //      need to be the right move button (right arrow, D, ...)
         Vector3 moveVector = (transform.forward * v) + (transform.right * h);
         moveVector *= speed * Time.deltaTime;
     
         // 3. Apply the mouvement to the local position
         transform.localPosition += moveVector;
     }

With this input you'll have a smoothed mouvement, if you want it sharp change the axis parameters in Edit > Project Settings > Input

Bye !

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

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Predictive Trajectory Issues 0 Answers

Using coroutine and texture array to randomly associate textures? 1 Answer

Get Direction From Raycast 0 Answers

How do i create sub meshes? 1 Answer

Rotate camera and player with mouse 2d top-down shooter in C# 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