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 /
avatar image
0
Question by rabione · Jan 10, 2017 at 07:46 AM · 3d3rd person controller

Players moves with simple script, but dont look where he moves

Hello all.

Sorry for my stupid question; my little script allows player to move in X and Z axis.

But he don't look where he moves...


public class PlayerMovement : MonoBehaviour { Animator anim; Rigidbody playerRigidbody; Vector3 movement;

 void Awake()
 {
           anim = GetComponent<Animator>();
     playerRigidbody = GetComponent<Rigidbody>();

 }

 void Animating(float h, float v)
 {
    bool walking = h != 0f || v != 0f;

    anim.SetBool("IsWalking", walking);
 }
 void FixedUpdate()
 {

     var x = Input.GetAxis("Horizontal") * Time.deltaTime * 15.0f;
     var z = Input.GetAxis("Vertical") * Time.deltaTime * 15.0f;
     
     transform.Translate(x, 0, z);

     Animating(x,z);
 }


     

}

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

2 Replies

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

Answer by Mikael-H · Jan 10, 2017 at 09:09 AM

You only tell the transform to translate in the direction you move. You also need to explicitly tell it to turn towards that direction. You can do this with the LookAt-method which will turn a transform to face a specific position:

 transform.LookAt(transform.position + new Vector3(x, 0, z));

EDIT:

Now that your character is turning you also need to take into account that the method signature for Translate() is:

 public void Translate(Vector3 translation, Space relativeTo = Space.Self);

This means that if you do not att the second parameter it will assume you want to move in the transform's local space, i.e. positive x axis is the right hand side of your character. If this is not the intended behaviour then you should write this instead:

 transform.Translate(new Vector3(x, 0, z), Space.World);

Personally, I prefer to always specify which space to use as I tend to forget which one is the default :)

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

Answer by rabione · Jan 10, 2017 at 10:12 PM

Thank you so much for your quick reply !

I've tried this script, and now thr player moves only in 2 directions :

With Up or Down button : he moves diagonally to down of the screen. with left or right : he moves diagonally to up of the screen.

(if you imagine what i'm sayin')

Thank you and sorry for this question.

Comment
Add comment · Show 15 · 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 Scoutas · Jan 10, 2017 at 10:22 PM 1
Share

It's because the translate method has a parameter, which asks if you want to translate the object in Local Space, or in World Space. By default it's set to Space.Self.

transform.Translate(x, 0, z, Space.World);

this should fix your problem, I think.

avatar image Mikael-H Scoutas · Jan 11, 2017 at 07:12 AM 0
Share

@Scoutas is absolutely right, I added some explanation in my answer. If this works for you then please mark the answer as correct. Also, please do not add an answer if you need clarification, add it as a comment or edit the original question. That way it will be easier for someone else who has the same problem in the future to find the correct answer to the question.

avatar image rabione · Jan 11, 2017 at 06:44 PM 0
Share

Thank you so much!

With these 2 methods, the player keeps moving diagonally:

Not Up/down/left/right, but diagnoally...

Sorry and thank you for your time!

Now it looks like this :

void FixedUpdate() {

     var x = Input.GetAxis("Vertical") * Time.deltaTime * 20.0f;
     var z = Input.GetAxis("Horizontal") * Time.deltaTime * 20.0f;
     
     transform.Translate(new Vector3(x, 0, z), Space.World);
     transform.LookAt(transform.position + new Vector3(x, 0, z));

     Animating(x,z);
 }
avatar image Mikael-H rabione · Jan 11, 2017 at 08:15 PM 0
Share

What happens if you comment out Animating(x,z) ? $$anonymous$$aybe there is an issue with root motion being turned on in the animator.

avatar image rabione · Jan 11, 2017 at 10:09 PM 0
Share

It simply stops player's animation... He walks diagonally without "walk" motion.

avatar image Mikael-H rabione · Jan 12, 2017 at 06:44 AM 0
Share

As the code should be correct, and it's not the animator settings then I suspect your input settings might be wrong. Can put their values in a Debug.Log statement and see that you actually get the vvalues through controls that you expect?

avatar image rabione Mikael-H · Jan 12, 2017 at 10:40 PM 0
Share

I don't know how to do this x) (debug log and stuff...)

I will check my default inputs in Unity.

avatar image Scoutas · Jan 12, 2017 at 12:13 AM 0
Share

$$anonymous$$ind posting your code here again?

I would love to see the inspector view of the player as well.

I just implemented this code into my own scene and it works perfectly (even the rotations look pretty). Anyway, just elaborate a little bit more, tell me what's attached to what and post the movement script in full.

avatar image rabione Scoutas · Jan 12, 2017 at 10:52 PM 0
Share

The script has no dependencies.

It runs on a 3rd person shooter. The player is freezed in Y position and X,Z rotation.

The inputs in Unity are correct.

The player now moves like this :

http://img15.hostingpics.net/pics/899525Sanstitre.jpg

Here's the full script :

using UnityEngine;

public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { Animator anim; Rigidbody playerRigidbody; Vector3 movement;

 void Awake()
 {
     anim = GetComponent<Animator>();
     playerRigidbody = GetComponent<Rigidbody>();

 }

 void Animating(float h, float v)
 {
    bool walking = h != 0f || v != 0f;

    anim.SetBool("IsWalking", walking);
 }
 void FixedUpdate()
 {

     var x = Input.GetAxis("Vertical") * Time.deltaTime * 20.0f;
     var z = Input.GetAxis("Horizontal") * Time.deltaTime * 20.0f;
     
     transform.Translate(new Vector3(x, 0, z), Space.World);
     transform.LookAt(transform.position + new Vector3(x, 0, z));

     Animating(x,z);
 }


     

}

avatar image Scoutas rabione · Jan 12, 2017 at 11:01 PM 0
Share

Your camera is rotated?

Show more comments
avatar image Scoutas · Jan 12, 2017 at 11:32 PM 0
Share

The reason I ask, if the camera is rotated, is because with translating in the world space, the player moves along the worlds x, y and z axis. If you camera is rotated by 45 degrees, that's the behaviour I would expect to be happening.

avatar image rabione Scoutas · Jan 12, 2017 at 11:35 PM 0
Share

I understand.

But the camera is not rotated. It faces the back of the player (when he's not moving) perfectly.

And why the Right and Left button are inverted + diagnoal?

I'm stuck at this point :/

avatar image Scoutas rabione · Jan 12, 2017 at 11:41 PM 0
Share

It is extremely weird. What about trying to create a new scene and just applying this script to a simple cube object and see if it works? Because it worked for me, I have no clue why it wouldn't work for you.

Show more comments

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

6 People are following this question.

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

Related Questions

How Do I Make a Manual Path in which I can Manually, and not Automatically, Go to a Destination and Back? 0 Answers

3rd person carrying objects,3rd person picking up objects 0 Answers

Create clip button not showing. 0 Answers

Object is not following parent bone. 0 Answers

Input Axis LeftAnalogHorizontal is not setup. 3 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