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 werseburg · Jul 22, 2013 at 09:07 PM · animationcontrollercs

Making a non player character move forward

How can I animate a character to constantly move in some vector3 direction, I know hot to make a mecanim character controller / how to make it move forward

 /// This script moves the character controller forward 
 /// and sideways based on the arrow keys.
 /// It also jumps when pressing space.
 /// Make sure to attach a character controller to the same game object.
 /// It is recommended that you make only one call to Move or SimpleMove per frame.    
 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 private var moveDirection : Vector3 = Vector3.zero;
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller.isGrounded) {
         // We are grounded, so recalculate
         // move direction directly from axes
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                 Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
 
       
     }
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
 
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
 }




I have been thinking about something like that:

     transform.TransformDirection(Vector3.up * Time.deltaTime, Space.World);    

Maybe there is some easier solution to this, can't come up with anything

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

3 Replies

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

Answer by growling_egg · Jul 22, 2013 at 10:51 PM

Are you looking for something more complex than this?

 var speed : int = 1; // Adjust to make your NPC move however fast you want.

 function Update ()
 {
   transform.Translate(Vector3(forward) * speed * Time.deltaTime);
 }
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 werseburg · Jul 23, 2013 at 03:24 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class movement : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
 int speed = 1; // Adjust to make your NPC move however fast you want.
  
 
 void Update ()
 {
   transform.Translate(Vector3.forward * speed * Time.deltaTime);
 }
     
     }
avatar image werseburg · Jul 23, 2013 at 03:24 PM 0
Share

$$anonymous$$y compiler went rogue and did not want to compile anything lol, I can only do C# scripts. That is the outcome and it is the solution to my problem, Thanks!

avatar image
0

Answer by fransh · Jul 22, 2013 at 10:48 PM

If you just want it to move forward, and you want it to move the walk speed given in that var speed, simply put down:

transform.Translate(Vector3.forward speed 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 werseburg · Jul 23, 2013 at 03:25 PM 0
Share

Thanks, I went for the growling_eggs solution

avatar image
0

Answer by IgorAherne · Jul 22, 2013 at 10:06 PM

you can move anything that has a js script attached. In that script, delete anything and write this:

 function FixedUpdate(){
 
 transform.Translate(0,0,0.2); //this moves an object that has this script forward along z axis
 }
 
 //fixedUpdate is by default every 0.02 seconds so your character will be moving 10 unsits a sec
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 IgorAherne · Jul 22, 2013 at 10:08 PM 0
Share

you could also do

 function Update(){
 
 transform.Translate(0,0,10)*Time.deltaTime;
 }
avatar image werseburg · Jul 23, 2013 at 03:25 PM 0
Share

Thanks for help can't give a like though :)

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

18 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

Related Questions

Can the mecanim animators Controller var be set in code? 1 Answer

Using Override Animation Controllers (NOT WORKING) 0 Answers

How do I animate a hopping character to fixed distances? 0 Answers

accessing charactercontroller 1 Answer

Create new Mecanim parameter from code? 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