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 john-essy · Sep 19, 2012 at 11:06 AM · character controller

Character Movement not moving how i want

Hi there and thanks for looking. I have some code right now that kinda works how i want. I want it to strafe left and right go back and forth. Things is when i press W he will always go forward no matter where my camera is facing. I want it to be where he fill go in the direction my camera is facing.

I am trying to get this controller to be like Ratchet And Clanks character controller so any help on how to do this or just help me with the above would be amazing.

I am trying to learn movements as they confuse the shizzle out of me. I do understand code very well but when it comes to this kinda thing it just goes over my head so any help with this would also be amazing.

Here is the code

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     private CharacterController controller;
     private Vector3 moveDirection;
     private Vector3 forward;
     private Vector3 right;
     public float speed;
     // Use this for initialization
     void Start () {
 
         controller = gameObject.GetComponent<CharacterController>();
         moveDirection = Vector3.zero;
         right = Vector3.zero;
         forward = Vector3.zero;
     }
     
     // Update is called once per frame
     void Update () {
         // anhance this but leanr more as you go forward. Learn each statement so you know what it is 
         forward = Vector3.forward;
         right = new Vector3(forward.z, 0, -forward.x);
         float horizontalInput = Input.GetAxisRaw("Horizontal");
         float verticalInput = Input.GetAxisRaw("Vertical");
 
         Vector3 targetDirection = horizontalInput * right + verticalInput * forward;
         moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 * Mathf.Deg2Rad * Time.deltaTime, 1000);
 
         Vector3 movement = moveDirection * Time.deltaTime * speed ;
         controller.Move(movement);
         Mathf.Clamp(0,0,0);
         float lookRot = Input.mousePosition.z;
         if (targetDirection != Vector3.zero)
         {
             
             Vector3 relativePos = transform.position - Camera.main.transform.position;
             float x, y, z;
             x = relativePos.x;
             y = 0;
             z = relativePos.z;
             Vector3 look = new Vector3(x, y, z);
             Quaternion rotation = Quaternion.LookRotation(look * Time.deltaTime * 2);
 
             transform.rotation = rotation;
         }
     }
 }
 
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
1
Best Answer

Answer by Khada · Sep 19, 2012 at 11:45 AM

Replace things like:

 forward = Vector3.forward;
 right = new Vector3(forward.z, 0, -forward.x);

With:

 forward = transform.forward;
 right = transform.right;

Also, use the inverse of these for back/left:

 backwards = -transform.forward;
 left = -transform.right;


'Vector3.forward' will always return the same value, in this case (0,0,1). This means that 'transform.position += Vector3.forward;' will always move your object down the Z axis. As such, the direction variables within the Vector3 struct are always treated as world/global directions.

When you want the relative forward direction of an object (emphasis on relative), you obtain this from the objects transform as these values are unique to the object and so are contained within the objects unique transform information. 'transform.forward' will always be the forward facing direction of the object in question.

Comment
Add comment · Show 5 · 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 john-essy · Sep 19, 2012 at 05:21 PM 0
Share

so i could say something like Direction = player.transform.forward and this will make something go in the players forward direction? If so the main thing that confuses the crap outa me are rotations also. I mean Quaternion.Euler WTF!!!:(

avatar image Khada · Sep 19, 2012 at 05:23 PM 0
Share

The common way of moving something in a forward direction is this:

 transform.position += transform.forward * Time.deltaTime * m_Speed;
 //where speed if how fast you want it to travel.
avatar image john-essy · Sep 19, 2012 at 08:07 PM 0
Share

I get it so i am adding 1 to my current position in the forward direction then getting my speed. I get all the Time.deltaTime and stuff i just feel like a noob right now lol i knew this day would come though where i need to learn this. Thanks mate if you have any other tips on directions maybe some little secrets or good ways of thinking on how to do it including rotations would be awesome lol :):):) Chancing my luck here you got Skype?

avatar image Khada · Sep 19, 2012 at 08:40 PM 0
Share

Rotation is a whole different ball game and a question unto itself. I could give you my skype, but then how I would I earn that sweet, sweet karma? And as a $$anonymous$$cher, I'm well disciplined at not giving personal contact info out. If I did, I wouldn't have time to breath. Just throw your questions up on here when you get stuck. Cheers :)

avatar image john-essy · Sep 19, 2012 at 11:58 PM 0
Share

lol i know what you mean lol i wouldnt stalk you :')

avatar image
0

Answer by john-essy · Sep 19, 2012 at 05:10 PM

Yea i get what you are saying thanks man, Thing is i still don't understand how it all works all i know is it kinda works. I want to know as much as possible with all this movement lark :P

Comment
Add comment · Show 3 · 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 Khada · Sep 19, 2012 at 05:12 PM 0
Share

I'll add some info to my answer. You should post this as a comment though, not an answer.

avatar image john-essy · Sep 19, 2012 at 05:18 PM 0
Share

Crap i thought i did it as a comment.

avatar image Khada · Sep 19, 2012 at 05:20 PM 0
Share

Np, I added the info you asked for.

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

10 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

Related Questions

How can I rotate a character 180 degrees by D, and -180 by A 2 Answers

Localscale-flipped 2D character retains original rotation since 5.4 2 Answers

How to be tangent with the ground without a character controller? 2 Answers

How to get my character to collide with objects 1 Answer

character controller height transition 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