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 RenatoRak · Jan 22, 2019 at 06:01 PM · movementtransformpositionplayer

Move Character in the way he is facing.,Make character move in way he is facing.

Hello, I have been strugling with this problem for a long time and I dont know the solution. So currently my character is moving in same directions e.g. Press W move up and when I press right he goes right but when i press W again he goes in that before same way before. So how can I make my character move/orient in the way he is facing? `

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveBrada : MonoBehaviour
 {
     private CharacterController characterController;
     private Animator animator;
     Vector3 moveDir = Vector3.zero;
 
 
 
     [SerializeField]
     private float moveSpeed = 100;
     [SerializeField]
     private float turnSpeed;
 
     private void Awake()
     {
         characterController = GetComponent<CharacterController>();
         animator = GetComponentInChildren<Animator>();
     }
 
     // Update is called once per frame
     private void Update()
     {
         var horizontal = Input.GetAxis("Horizontal");
         var vertical = Input.GetAxis("Vertical");
 
         var movement = new Vector3(horizontal, 0, vertical);
 
         characterController.SimpleMove(movement * Time.deltaTime * moveSpeed);
         animator.SetFloat("Speed", movement.magnitude);
 
         if (movement.magnitude > 0)
         {
 
             Quaternion newDirection = Quaternion.LookRotation(movement);
 
             transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSpeed);
 
     }
         
         }
     }
 
 

,So I am new at Unity Engine and I started making a shooter game and I simply cant make my character move in way he is facing. With this code he is moving like straight: using W he goes straight in 1 way always. How to fix this making him go in way he is facing or backing up in opposite way he is facing and so.?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveBrada : MonoBehaviour
 {
     private CharacterController characterController;
     private Animator animator;
     Vector3 moveDir = Vector3.zero;
 
 
 
     [SerializeField]
     private float moveSpeed = 100;
     [SerializeField]
     private float turnSpeed;
 
     private void Awake()
     {
         characterController = GetComponent<CharacterController>();
         animator = GetComponentInChildren<Animator>();
     }
 
     // Update is called once per frame
     private void Update()
     {
         var horizontal = Input.GetAxis("Horizontal");
         var vertical = Input.GetAxis("Vertical");
 
         var movement = new Vector3(horizontal, 0, vertical);
 
         characterController.SimpleMove(movement * Time.deltaTime * moveSpeed);
         animator.SetFloat("Speed", movement.magnitude);
 
         if (movement.magnitude > 0)
         {
 
             Quaternion newDirection = Quaternion.LookRotation(movement);
 
             transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSpeed);
 
     }
         
         }
     }
 
 

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 andzq · Jan 23, 2019 at 04:19 AM 0
Share

I sadly cannot see what your "Simple$$anonymous$$ove" - Function does, however, maybe

 transform.forward

and

 transform.right

can help you as these are the normalized vectors of the "forward" and "right" direction of your transform. You can use them to precicely move your character forward/backward and left/right. I tested it in a little scene by adding the following script to a little cube.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Simple$$anonymous$$ove : $$anonymous$$onoBehaviour
 {
     public float rotationSpeed = 30;
     public float movementSpeed = 3;
 
     void Update()
     {
         RotateWith$$anonymous$$eys();
         $$anonymous$$ove();
     }
 
     void RotateWith$$anonymous$$eys()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
         {
             transform.Rotate(Vector3.up, Time.deltaTime * -rotationSpeed);
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
         {
             transform.Rotate(Vector3.up, Time.deltaTime * rotationSpeed);
         }
     }
 
     void $$anonymous$$ove()
     {
         Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
 
         transform.Translate(Vector3.forward * input.y * Time.deltaTime * movementSpeed);
         transform.Translate(Vector3.right * input.x * Time.deltaTime * movementSpeed);
     }
 }





1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Jan 23, 2019 at 09:18 AM

Good day.

I think you are lookig for this:

Transfomr.translate()

This function moves in LOCAL coords (1,0,0) is always "ahead", because is using relative coords (oposite of global). In Unity you need always to think if using global coords or local coords.

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

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

255 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Player teleport not working 0 Answers

How to move gameObject along a Vector3 with OnMouseDrag()? 0 Answers

How to constrain first person player to a path? 0 Answers

Locking an objects movement on a desired axis, disregarding any external forces. 0 Answers

How to properly make an object follow the edge of another object? 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