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 djordr · May 07, 2018 at 08:45 AM · rotationinputvector3facebookdirection

Creating a simple character controller (Script provided)

I want a very simple character controller, a character that moves around in all directions and faces the direction he's moving in. But I'm confused about how to achieve that, I find vector3 stuff confusing in general. How would I make this code use force and clamp the speed of the character's movement and have him keep on facing the direction he's moving in. Right now after he stops moving the rotation is reset.

 public float speed;
     bool canPlayerMove = true;
 
     void Start()
     {
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
         float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
 
         if (canPlayerMove)
         {
             Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
 
 
             transform.Translate(movement * speed * Time.deltaTime, Space.World);
         }
     }
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 tormentoarmagedoom · May 07, 2018 at 11:01 AM 0
Share

Good day.

If you are "noob" with this issues, there are much more simple ways to achieve the simple movement you want.

Look for examples, will see is much more simple.

PD: there is a component called PlayerController, read some api and manuals too:D

BYE !

avatar image djordr tormentoarmagedoom · May 07, 2018 at 06:22 PM 0
Share

It's only as simple as how well it's explained. All of this is simple if you understand it, in my script above everything works except the rotation is reset when you stop moving. It's also moving around the transform which is not ideal. I have looked around and there seems to be no tutorial on how to make a character move and face the direction he's moving in, crazy. It's all either mouse controlled third person controllers or tank movement or something similar. The third person script that comes with unity works but it's also very messy and the comments inside the script itself don't explain much. BYE!

1 Reply

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

Answer by djordr · May 07, 2018 at 08:19 PM

Hello buddy, this doesn't work with forces but fixes you current code. Someone else would have to tell you about how to change this to use forces.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class scr : MonoBehaviour {
  
     bool canPlayerMove = true;
  
     public float speed = 0.0f;
  
     void FixedUpdate()
     {
         float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
         float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
  
         if (canPlayerMove)
         {
             Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
  
             if (movement != Vector3.zero)
             {
                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
             }
  
             transform.Translate(movement * speed * Time.deltaTime, Space.World);
         }
     }
 }
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 djordr · May 07, 2018 at 08:21 PM 0
Share

That is beautiful, now only if someone could explain how to add a force to this ins$$anonymous$$d of using the transform.position. Because it would be nice to have the character walk if you move the joystick just a little bit and run if you move him faster. Would it be as simple as to just change the last line of the script, I'm not sure.

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

189 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

Related Questions

change angle towards direction 1 Answer

Rotation resets on character controller 0 Answers

Object to walk forward, turn 180 and walk back 1 Answer

How do I rotate my object back to the world's up direction whilst keeping it's forward direction? 0 Answers

How to rotate an object around a player when pushing a button..? 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