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 Koramere3D · Feb 10, 2012 at 09:42 PM · javascriptcharacterforward

Moving Forward

Hi,

I was wondering what javascript code you can use to make a character go forward when the player hits "W", and back when he presses "S", and turns when he presses "A" or "D". Please comment the code so that I can see what makes what work.

Thank You for Your Time

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 Berenger · Feb 10, 2012 at 09:44 PM

Go in Asset->IMport Asset->Character Controller. Drag the 1st or 3rd Person Controller prefabs into your scene, play around, then look the scripts 3rd or FPSInputController. It's completely commented, but there is a lot to do. So grab a coffe, tell your friends they won't see you for a while, and go for it !

Comment
Add comment · Show 4 · 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 Koramere3D · Feb 11, 2012 at 06:19 PM 0
Share

Thank You, but I have a kind of personality characteristic. The Reason that I decided to use Unity ins$$anonymous$$d of Blender for game engine, is because that I don't like to use things that were made by someone else, because it feels like I am just slapping on code. I like to write my own code. Now this might seem a little stupid, because I was asking for someone to write a piece of javascript for me, but I wanted a simple script that I could mess around with. You did not know this, so thank you for your help.

avatar image Berenger · Feb 11, 2012 at 06:39 PM 0
Share

Fair enough. So let's focus on the heart of those script I was talking about. Their point is to make the movement smooth, control the jump, the collisions, the animations and other stuff. If you just want the movement, it's pretty simple (Yihaaa, I managed the code formatting in a comment !!) :

var speed : float = 100f;

function Update(){
    var v, h : float;
    v = Input.GetAxis( "vertical" ); // Up and down button
    h = Input.GetAxis( "horizontal" ); // Left and right
    if( $$anonymous$$athf.Abs(v) > 0f || $$anonymous$$athf.Abs(h) ){ // Is the player pressing a key ?
        var direction : Vector3;
        direction = Vector3( h, 0, v ).normalized;
        // Set the direction in local space. The rotation isn't implemented here
        direction = Transform.InverseTransformDirection( direction );
        direction *= speed * Time.deltaTime; // Adjust speed
        transform.position += direction; // Classique
        characterController.Simple$$anonymous$$ove( direction ) // CharacterController. Use only one ;)
    }
}

avatar image Koramere3D · Feb 11, 2012 at 10:18 PM 0
Share

That is good for my purposes, But is there a way to specificly "say" that a certain key triggers a certain fuction? Thank You

avatar image Berenger · Feb 11, 2012 at 10:46 PM 0
Share

It's not possible natively to map a function on a key. However, you can know if a key is pressed or not with the Input class. If you get a positive, you can call the corresponding function.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

"Teleport" in the way the player is heading 2 Answers

How can I tell Unity to wait to do something until after and animation has finished playing? 1 Answer

Problem with PlayerHealth and renderer 1 Answer

How can I activate a script upon entering an area? 1 Answer

Basic Movement on a Plane without Character Controller 2 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