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 tonio1206 · Sep 28, 2014 at 05:05 PM · javascriptgame

How to make my character always run in the same direction?

I'm trying to make a game such as Subway Surfer or Temple run. But with my script, the run animation works but my character is not moving. Here is my scipt:

 var MainCharacter : CharacterController;
 
 var MoveDirection:Vector3;
 
 var speed : float = 100;
 
 var runSpeed : float = 6.0;
 
 public var runAnimation : AnimationClip;
 
 public var runMaxAnimationSpeed : float = 1.0;
 
 private var _animation : Animation;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
  
 function Update()
 {
      
         if(!runAnimation) 
         {
         _animation = null;
         Debug.Log( AnimationClip);
         }
         
 }
 Could you please help me

thanks

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
0

Answer by SterlingStudios · Sep 28, 2014 at 05:24 PM

This should work.

 var speed : float = 6;
 var direction : Vector3 = Vector3.fwd;
 
 var runAnimation : AnimationClip;
 
 function Update () {
 
     transform.Translate(direction * speed * Time.deltaTime);
 
     animation.Play(runAnimation.name);
 }

Warning: Not Tested Code

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 tonio1206 · Oct 03, 2014 at 04:43 PM 0
Share

Now, how could I make my character turn? Thanks for the help

avatar image N1warhead · Oct 04, 2014 at 12:41 AM 0
Share

Or you can just do.

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){ transform.Rotate(0,-5,0); }

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){ transform.Rotate(0,5,0); }

Not sure what temple run is or anything, but that's another alternative to rotating.

And if you want to use Rigidbody Physics to move forward and stuff just add a Rigidbody to your Player. (Change chage the Vector3.forward) to the directions you want and you will move that direction.

and add this in a FIXEDUPDATE

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)){ transform.rigidbody.AddRelativeForce (Vector3.forward * 10);

EDIT - I noticed again that your thread said ALWAYS run in same direction.. In update just put transform.Translate(Vector3.Forward); or whatever direction you want him to go.

avatar image tonio1206 · Oct 05, 2014 at 08:05 PM 0
Share

Thanks for your answers. both scripts works.

avatar image tonio1206 · Oct 06, 2014 at 07:17 PM 0
Share

the problem with both scripts is that my character diseppears and and then reappears in the right place. We can't see him turn. I don't know if you've understand but how could I solve this? thanks

avatar image
0

Answer by SterlingStudios · Oct 03, 2014 at 10:14 PM

Considering you mean like a temple run move, you could try this (add it in on line 11 or 12):

 if(Input.GetKeyDown(KeyCode.LeftArrow)) {
     transform.position.x -= turnAmmount;
     //cant remember if its x or z, so if x doesnt work try z
 }
 
 if(Input.GetKeyDown(KeyCode.RightArrow)) {
     transform.position.x += turnAmmount;
     //cant remember if its x or z, so if x doesnt work try z
 }

And add some new variables:

 var turnAmmount : float = 2;

Again, untested code.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Does Anyone Have a Halo-Like Scripts? 1 Answer

Already has a definiton for 1 Answer

How to create a Game Tree data structure in Unity. 0 Answers

Javascript error.. insert semicolon?? 2 Answers

getting udp package info inside unity (GlovePIE) 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