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 /
  • Help Room /
avatar image
0
Question by KasunL · Nov 25, 2015 at 02:29 PM · movement scriptmove an object

Object Movment problem (3D)

Hi all,

I've attempted creating 2 games with Unity so far -- one is 2D other is 3D. In my 2D game, i haven't had any problems with moving objects.

Here's my code to move my player object to left, on left UI button Click:

 float moveSpeed = 2f;
 .......
 .......
 public void SheWalksL()
 {
      AniWalkL (); // Animation
 
      GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D> ().velocity.y); // Move Left
 }

Above code is working on UI Button press. But in my 3D game, when i try to move my player object in the same way (with the added 3rd dimension), it's not working. Here's the code:

 public void ButtonMoveL()
 {
   Vector3 movemnt = new Vector3(-2.0f, GetComponent<Rigidbody>().velocity.y, 0);
   GetComponent<Rigidbody>().velocity = movemnt * speed; // Move Left (Vantage point: looking down at the scene, along the Y-axis. So, Z is aligned vertically and X is aligned horizontally)
 }

Only difference is that in the 3D game, i've created the movement vector separately and assigned it to the object velocity, and provided the X value of the vector directly without using a variable. I've also got a "Fire" button, which functions perfectly when pressed, so it can't be a problem with the button event call. Could someone please point out what i've done wrong in the 3D code to make it to not working.

Thanks for any input!

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 Baby_Dino · Nov 25, 2015 at 05:09 PM

@KasunL:

Hmmmm... I plugged your movement code into my own 3d game that I am creating and it worked, check to make sure that you have a Rigidbody (not Rigidbody2d) attached to your game object and double-check to make sure that the function is actually being called.

By the way, this is usually a better way to move an object in 3d: transform.Translate(Vector3.left * Time.deltaTime*speed); that will move the object that is attached to the script to the left. You should be able to put that in your function and make the object move (assuming that the function is called).

Tell me if that works

Comment
Add comment · Show 3 · 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 KasunL · Nov 25, 2015 at 06:20 PM 0
Share

Thanks for the reply.

The Translate code kind of worked, thanks, (and i donno why .velocity is not working), but the problem is .Translate moves the object as if it's "cutting and pasting" the object at a different position -- not a smooth move, rather like a jump. So I need to press the button continuously to make the ship 'hop' at least. I donno if .velocity is working as same as this -- it never worked for me yet. What I want is to move the object smoothly when I press and hold the button. Can .Translate do that if I implement it inside a "press and hold" button event -- wouldn't it still 'hop' as I hold the button -- I think it will.

Also I don't know how to implement a "press and hold" button event. Could you please shortly describe how to do that? -- so I could put your code inside it and test how the object move.

$$anonymous$$asun

avatar image Baby_Dino · Nov 26, 2015 at 02:40 AM 0
Share

Ok, here is how you use velocity in Unity5: GetComponent().AddForce(transform.right * 30); See if that works, if it doesn't, I will look into how to implement a "press and hold" button event (I don't use buttons in my games, I use the arrow keys, mouse, etc. so I don't know much about how they work yet).

avatar image KasunL Baby_Dino · Nov 26, 2015 at 03:57 AM 0
Share

Hi

Actually i've implemented addForce earlier(which also not worked at all), like this:

 GetComponent<Rigidbody>().AddForce(movemnt * speed);

Your code for addForce returned an error:

 The type arguments for method `UnityEngine.Component.GetComponent<T>()' cannot be inferred from the usage.

btw, with $$anonymous$$B key's the code works pretty fine. It's when i implemented the code with a button that this bug appear. (weird ha. ???)

I'll try to find out how to implement "press and hold".

And, Thanks for continues support :)

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

Prefab 2d interaction with 3d prefab 0 Answers

How do I get a character to walk on walls and ceilings? 1 Answer

Move a selected object. 0 Answers

My bullet keeps following its spawn point after being instantiate. Pls help 1 Answer

my box movements look jitter, how to fix it 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