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 TrivialRoo · Sep 06, 2014 at 08:05 PM · rotationmovementplayer

Rotation for movement cannot be set in script?

Hello. So in my playermovement script I have this line:

 direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical") );

And it doesn't move me in the direction that I'm facing. Why is this?

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
0

Answer by zharik86 · Sep 06, 2014 at 08:24 PM

In most cases player is directed by the face on an axis z. Then GetAxis define the new direction. Then write so(write on CSharp):

  void Update() {
   Vector3 relativeDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
   Vector3 absoluteDirection = transform.rotation * relativeDirection;
   transform.position += absoluteDirection * Time.deltaTime;
  }

I hope that it will help you.

Comment
Add comment · Show 2 · 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 TrivialRoo · Sep 07, 2014 at 12:03 AM 0
Share

Alright this is my whole script:

 using UnityEngine;
 using System.Collections;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
 
     /*
      * 
      *                 This component is only enabled for "my player" (i.e. the character belonging to the local client machine)
      * 
      * 
      */
     
     float speed = 10f;
     float jumpSpeed = 6f;
     Vector3 direction = Vector3.zero;    // forward/back & left/right
     float   verticalVelocity = 0;
     
     CharacterController cc;
     Animator anim;
     CapsuleCollider capsCollider;
     float realCapsuleColliderHeight = 2f;
     float jumpCapsuleColliderHeight = 1.2f;
     
     
     
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
         capsCollider = (CapsuleCollider)collider;
     }
     
     // Update is called once per frame
     void Update () {
         
         // WASD forward/back & left/right movement is stored in "direction"
         Vector3 relativeDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
         direction = transform.rotation * relativeDirection;
 
         if(direction.magnitude > 1f) {
             direction = direction.normalized;
         }
         
         anim.SetFloat("Speed", direction.magnitude);
         
         // Handle Jumping
         
         if(cc.isGrounded) {
             anim.SetBool("Jumping", false);
             //capsCollider.height = realCapsuleColliderHeight;
             
             if(Input.GetButton("Jump")) {
                 verticalVelocity = jumpSpeed;
             }
             else {
                 verticalVelocity = -1f;
             }
         }
         else {
             anim.SetBool("Jumping", true);
             //capsCollider.height = jumpCapsuleColliderHeight;
         }
         
         
     }
     
     // FixedUpdate is called once per physics loop
     // Do all $$anonymous$$OVE$$anonymous$$ENT and other physics stuff here.
     void FixedUpdate () {
         
         Vector3 dist = direction * speed * Time.deltaTime;
         
         if(cc.isGrounded) {
         }
         else {
             verticalVelocity += Physics.gravity.y * Time.deltaTime;
         }
         
         Debug.Log (verticalVelocity);
         
         dist.y = verticalVelocity * Time.deltaTime;
         
         cc.$$anonymous$$ove( dist );
     }
 }


It's only going forward in one direction.

avatar image zharik86 · Sep 07, 2014 at 07:01 AM 0
Share

@TrivialRoo The most part of a code at you is written for CharacterController. The first, delete your CapsuleCollider. Further, it isn't necessary to use function the FixedUpdate(). I will a little correct your script:

  using UnityEngine;
  using System.Collections;
 
  public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {

   float speed = 10f;
   float jumpSpeed = 6f;
   Vector3 dir = Vector3.zero;   // forward/back & left/right
   float myGravity = 10.0f; //variable for gravity

   CharacterController cc;
   Animator anim;

   void Start () {
    cc = GetComponent<CharacterController>();
    anim = GetComponent<Animator>();
    //If you want, here you can change variable gravity
    myGravity = Physics.gravity.y;
   }

   void Update() {
    //Change direction only on ground
    if(cc.isGrounded) {
     anim.SetBool("Jumping", false);
     //$$anonymous$$ost likely you expect to receive values +1 or-1, but magnitude returns vector length, therefore value always the positive.
     //Therefore you need necessary to invent a certain condition
     anim.SetFloat("Speed", direction.magnitude); //maybe wrong
     Vector3 relativeDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
     dir = this.transform.TransformDirection(relativeDirection);
     dir = dir * speed;
     if(Input.GetButton("Jump")) {
      dir.y = jumpSpeed;
      anim.SetBool("Jumping", true);
     }
    } else {
     anim.SetBool("Jumping", true);
    }
    dir.y -= myGravity * Time.deltaTime; //Always apply gravity
    cc.$$anonymous$$ove(dir * Time.deltaTime);
   }
  }

As that so.

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

22 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

Related Questions

Animator movement and mouse position problem 0 Answers

Player not rotating with camera 1 Answer

Player Move-Rotation 0 Answers

How do i rotate my player in moving direction in 2d? 0 Answers

transform.lookat snaping direction 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