• 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 RussianBsack · Aug 01, 2014 at 02:19 PM · transformpositioninput.getkeyvector3.position

Problem Vector3.transform (C#)

 using UnityEngine;
 using System.Collections;
 
 public class characterMovement : MonoBehaviour {
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKey(KeyCode.W)){
             transform.position += Vector3.forward * Time.deltaTime;
         }
     }
 }

Having a problem with that script and I'm not sure what I'm doing wrong as no errors or console messages are coming back so the script is activated and working but i don't understand why the functionality isn't working?

Also, is there any way in which i can link the mouseLook script to the character script? in other words, where the character looks with the mouse, thats what the player will follow? So following head movement?

Comment
Add comment · Show 6
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 robertbu · Aug 01, 2014 at 02:21 PM 0
Share

It is unclear where your problem is. The code above looks fine. It will move the object towards positive 'z' at the rate of 1 unit per second as long as the key is held down. A single click will only move it a small (and perhaps unnoticeable) distance . So maybe your scale is too big and 1 unit per second is doesn't show, or maybe you are using an orthographic camera, or maybe you are expecting a click to move it, or maybe the script is disabled, or....

If you are rotating this object, you can cause it to move in the direction of rotation by replacing Vector3.forward with transform.forward.

avatar image RussianBsack · Aug 01, 2014 at 02:42 PM 0
Share

Now i have the problem whereby my character keeps falling through the floor and i have lost the functionality of the left and back keys as there is only: transform.forward/right. No left or back?

By fall, i mean there is no sense of gravity so if i look upwards my character will move upwards and start floating, and if i look downwards my character will go through the floor.

avatar image robertbu · Aug 01, 2014 at 03:00 PM 0
Share

Assu$$anonymous$$g you are walking on the XZ plane, it sounds like you need to remove the 'y' component from your movement vector. Something like:

 Vector3 dir = transform.forward;
 dir.y = 0.0;
 dir.Normalize();
 transform.position += dir * Time.deltaTime * speed;

Note I've added 'speed' here which you don't need if you are happy with your current speed. 'speed' is a variable you define or can be replaced by a constant.

avatar image RussianBsack · Aug 02, 2014 at 08:24 PM 0
Share

I see, where exactly do i put that code? This is what i have so far.

 using UnityEngine;
 using System.Collections;
 
 public class character$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     
     // Update is called once per frame
     void Update () {
 
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)){
             gameObject.transform.position += transform.forward * Time.deltaTime;
         }
 
         Vector3 dir = transform.forward;
         dir.y = 0.0;
         dir.Normalize();
         transform.position += dir * Time.deltaTime;
 
     }
 }

But i keep getting errors when i try to play it, Do i create a new C# script? Also, If my terrain has slopes and stuff or if i want my character to Jump, do i not need the Y axis enabled?

avatar image robertbu · Aug 02, 2014 at 08:35 PM 0
Share

See that this is C#, not Javascript/Unityscript, you need to add an 'f' to the end of 0.0:

  dir.y = 0.0f;

For any future posts, it is really helpful if you include a copy of the error you are getting in the console for any compile-time errors.

avatar image RussianBsack · Aug 02, 2014 at 09:41 PM 0
Share

Ah damn yeah, i failed to see that. Thanks for the help.

1 Reply

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

Answer by SblandUK · Aug 02, 2014 at 08:37 PM

1st off take a look at the character controller scripts included in the standard assets.

If you want to add backwards movement use -transform.forward instead of transform.forward.

To use the above code put

  Vector3 dir = transform.forward;
         dir.y = 0.0;
         dir.Normalize();
         transform.position += dir * Time.deltaTime;

inside

  if(Input.GetKey(KeyCode.W)){
            
         }

Like so:

  if(Input.GetKey(KeyCode.W)){
         Vector3 dir = transform.forward;
         dir.y = 0.0;
         dir.Normalize();
         transform.position += dir * Time.deltaTime;
             
         }

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 RussianBsack · Aug 02, 2014 at 09:42 PM 0
Share

Thanks for clearing it up!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Removing Vector3 Decimal for Position 2 Answers

make player move in direction it's facing 2 Answers

How to find IndexOf vector3 List ? position search 2 Answers

What is the reasoning of multiplying by Time.deltaTime 1 Answer

does not work check if dead 1 Answer


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