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 latsushi · Oct 13, 2012 at 07:49 PM · c#movemoving

Get an Object to Automatically Move Forward

Hello,

I'm trying to get an object to move forward automatically while being constrained by collisions. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class MoveForward : MonoBehaviour {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     
     
     // Update is called once per frame
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
          if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
              if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);    
     }
 }

This is the exact same code from the Unity Scripting Reference in C#. You can find that here: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html

Can someone please tell me what I'm missing. I've been tackling this problem for, literally, weeks. Any input is appreciated. I thank you for your time.

Comment
Add comment · Show 2
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 Loius · Oct 13, 2012 at 08:31 PM 0
Share

What is you code doing, exactly? Is the thingy moving, but it goes through stuff, or what? The code looks alright at a glance.

avatar image latsushi · Oct 14, 2012 at 04:47 AM 0
Share

Loius, the problem is the object isn't moving. I can move the object when I press the appropriate buttons on my keyboard, but my goal is to get the character moving forward automatically.

2 Replies

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

Answer by Ghopper21 · Oct 14, 2012 at 05:34 AM

This line:

 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))

means you will move only when there is input, due to Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") being used for the x and z values of your movement direction.

To move forward automatically in a simplistic way, use hard-coded x and z values for the direction you want to move in.

If you want to still use input for movement direction but have the character continue to move forward when input has stopped, you can first check if there is input and if not use your existing direction for your movement direction.

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 latsushi · Oct 19, 2012 at 04:13 PM 0
Share

This is exactly what I needed. Thank you for your assistance sir.

avatar image
0

Answer by Montraydavis · Oct 14, 2012 at 12:24 AM

I may be getting the wrong idea, but, it sounds like something that can be easily done with OnCollisionStay ( ) .

Check if it's colliding with X, then move (x,y,z)

function OnCollisionStay ( collision : Collision) { if ( collision.collider == existingObjectToCheckFor ) {

  //do movement code

} }

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 latsushi · Oct 14, 2012 at 04:48 AM 0
Share

The problem is the object isn't moving. I can move the object when I press the appropriate buttons on my keyboard but my goal is to get the character moving forward automatically.

Forget collision right now. The object does collide. So right now I'm trying to get the object to move forward automatically. What do you think I need to do?

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can I move component of one gameobject to other. 3 Answers

moving platforms? 0 Answers

What's the using directive for Strings in C#? 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