• 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
1
Question by Mischa.Silden · Jan 22, 2012 at 03:21 AM · movementcharactergravitydirectionvector2

Making a character move automatically in one direction.

I want my character to move in one direction at all times (a running sidescroller game), and it still needs to use gravity.

I tried transform.Translate but that forces the character to go trough objects. Can someone help me? I'm still quite new to Unity.

 using UnityEngine;
 using System.Collections;
 
 public class scr_Player : MonoBehaviour {
     public float sspeed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector2 moveDirection = Vector2.zero;
 
     void Start () {
     }
 
     void Update () {
        // transform.Translate(0.10F, 0, 0 * Time.deltaTime);
             //Didn't work as intended.
                 //Forced the character through objects.
 
         CharacterController player = GetComponent<CharacterController>();
         if (player.isGrounded) {
             moveDirection = new Vector2();
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= jumpSpeed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
 
         }
         moveDirection.y -= gravity * Time.deltaTime;
         player.Move(moveDirection * Time.deltaTime);
 
 
     }
 }
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 syclamoth · Jan 22, 2012 at 08:14 AM 0
Share

You're on the right track, actually- you just need to add a 'Physics.Raycast' step that shoots out in front of where your character would get translated that frame. If it hits something, manage collisions, and if it doesn't, just do your translation like normal!

avatar image Mischa.Silden · Jan 22, 2012 at 09:05 AM 0
Share

Want to give me some directions? I just managed to crash unity for the first time.

using UnityEngine; using System.Collections;

public class scr_Player : MonoBehaviour { public float sspeed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector2 moveDirection = Vector2.zero;

 void Start () {
 }

 void Update () {
    // Moves the character forward.
    transform.Translate(0.10F, 0, 0 * Time.deltaTime);
         //Didn't work as intended.
             //Forced the character through objects.

    // Tells if there is something in front of the player.
    Vector2 forward = transform.TransformDirection(Vector2.right);
    if (Physics.Raycast(transform.position, forward, 5))
        // This crashes the game.
        Instantiate(collider);

     // Allows the character to jump.
     CharacterController player = GetComponent<CharacterController>();
     if (player.isGrounded) {
         moveDirection = new Vector2();
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= jumpSpeed;
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;

     }
     moveDirection.y -= gravity * Time.deltaTime;
     player.Move(moveDirection * Time.deltaTime);


 }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ByteSheep · Jan 22, 2012 at 08:04 AM

If you do not want to move the character via transform you could try adding force to the rigidbody of your character:

rigidbody.AddForce (0, 0, 5);

Just make sure that your character has a rigidbody component :)

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 Mischa.Silden · Jan 22, 2012 at 08:12 AM 0
Share

Thanks for your time but that didn't work :/

avatar image
0

Answer by Mischa.Silden · Jan 22, 2012 at 02:51 PM

Some nice guys at 4chan /v/ Gamedev Thread came up with a working solution, here it is:

public class scr_PlayerTest : MonoBehaviour { public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector2 moveDirection = Vector2.zero;

 void Start(){
 }

 void Update(){
     CharacterController player = GetComponent<CharacterController>();

     if (player.isGrounded)
     {
         // if player is on the ground, we normally don't want to move $$anonymous$$m vertically
         moveDirection.y = 0;

         // but if the jump button is pressed, then we do want $$anonymous$$m moving vertically
         if (Input.GetButton("Jump"))
         {
             moveDirection.y = jumpSpeed;
         }
     }
     else
     {
         // if player is not on the ground, then apply gravity to $$anonymous$$m
         moveDirection.y -= gravity * Time.deltaTime;
     }

     // constantly move horizontally
     moveDirection.x = speed;

     // finally, we actually apply the movement to the player
     player.Move(moveDirection * Time.deltaTime);
 }

}

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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

about character movement in 8 direction 1 Answer

Character float to the sky when moving to right using 'addforce' 1 Answer

Push player in direction based on objects movement 1 Answer

CharacterMotor Gravity Set 1 Answer

How to Rotate the Player In Direction of Movement? 4 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