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 outsiderman411 · Jan 31, 2012 at 06:59 AM · character

How do i make the game start so that the character is already running?

This is my code so far, i was wondering if there is a way to make the character start off the game running?

using UnityEngine; using System.Collections;

[RequireComponent (typeof(CharacterController))] public class AdvancedMovement : MonoBehaviour { public float walkSpeed = 5; public float runMultiplier = 2; public float strafeSpeed = 2.5f; public float rotateSpeed = 250; public float gravity = 20;

 public CollisionFlags _collisionFlags;
 private Vector3 _moveDirection;
 private Transform _myTransform;
 private CharacterController _controller;
 
 public void Awake() {
     _myTransform = transform;
     _controller = GetComponent<CharacterController>();
 }
 
 // Use this for initialization
 void Start () {
     _moveDirection = Vector3.zero;
 
 }
 
 // Update is called once per frame
 void Update () {
     if(_controller.isGrounded) {
         Debug.Log("On the ground.");
         
         _moveDirection = new Vector3(0,0, Input.GetAxis("Move Forward"));
         _moveDirection = _myTransform.TransformDirection(_moveDirection).normalized;
         _moveDirection *= walkSpeed;
         
     }
     else{
         Debug.Log("Not on the gorund.");
         
         if((_collisionFlags & CollisionFlags.CollidedBelow) == 0) {
         }
     }
     _moveDirection.y -= gravity * Time.deltaTime;
     _collisionFlags = _controller.Move(_moveDirection * Time.deltaTime);
 }

}

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 senad · Jan 31, 2012 at 08:28 AM

In your first Update-method call you will first check the user-input and then update the movement. It is very likely that there is no user input in the first update frame, so your character will not move.

So as I see it, if you want your character to move from the start, you need to set a moveDirection in the Start()-method and ignore the user input while it is zero.

This way your character would be moving from the beginning until the player changes the direction. Is it what you want to happen?

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 outsiderman411 · Jan 31, 2012 at 04:54 PM 0
Share

In a way yes, I am trying to make it so that throughout the whole game the user has no control over the forward movement of the character but only has the option to move from side to side and to jump. Does that make any sense?

avatar image senad · Feb 01, 2012 at 09:07 AM 0
Share

Yes, now I get your meaning. :)

How you can implement this actually depends on your camera. If your camera is rotating then your forward moving direction will change with it. In this case it will always be the direction of the cam but with y = 0. The left vector will be the cross product of the forward with the up vector.

If your camera rotation is fixed, you will always have constant direction vectors for forward and left. You can use vector addition to calculate your movement like this: movement = forward + Input.GetAxis("leftright") * left;

An example for a constant setup: forward = (0,0,1) left = (-1,0,0) up = (0,1,0)

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

Multiple Cars not working 1 Answer

hide object start script 4 Answers

Can somebody help me out with a jumpscare? (Like from the game "Slender") 1 Answer

Need Help Fixing WeaponScript *(cant find compiler error) 1 Answer

On Collision with water buoyancy and play Swim animation 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