Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Claymstamper · May 03, 2016 at 05:25 PM · 2dmovementscript.

Why is this code not making my 2D sprite move?

using UnityEngine; using System.Collections;

public class Ninja : MonoBehaviour {

 float speed = 1.0f;

 // Use this for initialization
 void Start () {
     transform.position = new Vector3 (2, 3, -5);

 
 }

 // Update is called once per frame
 void update () {

 }
 void faceRight () {
     transform.localRotation = Quaternion.Euler (0, 0, 0);
 }
 void OnCollisionEnter2D(Collision2D coll){
     // If the Collider2D component is enabled on the object we collided with.
     if (coll.collider == true) {
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         Debug.Log ("Weston sucks eggs");

     }
 }

}

Comment
Add comment · Show 1
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 Cherno · May 03, 2016 at 05:35 PM 0
Share
  1. Please format your code properly.

  2. Here is what happens: The script registers a collision in one single frame (you are using OnCollisionEnter, not OnCollisionStay. If it also registers input, the transform.posiiton is changed for that single frame.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Aliwan · May 03, 2016 at 06:06 PM

I'm not an expert, but I think that you need to write your code within the FixedUpdate() method (because its related to physics and you need to test your condition every frame, note just once).

Also, i think you need to get the rigidbody2d component and apply force to it in order to move your gameobject. Something like:

 // Declaration
 
 private Rigidbody2D rb;
 
 void Awake()
 
 {
 
 //asign rigidbody2d to the variable rb
 rb = getComponent<Rigidbody2d>();
 
 }
 
 
 void FixedUpdate
 
 {
 //apply force to that rigidbody
 rb.addforce (vector3/*..... and all sort of parameters you want to apply to it */);
 }

I'm maybe wrong because im a complete beginer. Hope it helps though.

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
avatar image
0

Answer by ThisTestWillDo · May 04, 2016 at 12:34 AM

As @Cherno said, you need to change OnCollisionEnter2D to OnCollisionStay2D, otherwise you would need to be already providing the input at the instant the object collided, and you would only make a single frame's worth of movement. Also, there's no reason to check if the collider is enabled, as you won't receive the OnCollision message if it's not.

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

6 People are following this question.

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

Related Questions

How to Convert each 2D Array to GameObject 2 Answers

Moving object disappears then comes back 1 Answer

Top down 2d character controller sometimes walking backwards 1 Answer

[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers

Stop Moving When Not Touching Screen? 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