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 kevinv321 · Jul 11, 2013 at 09:18 PM · cameraplayer

Help with movement cube

Hello all, I have some movement problems with a simple cube object.

The camera position is above the terrain, with a rotation of x = 55 and faces the Z axis. This camera will always be faced this direction so the rotation is always the same, however the position can vary (when the cube moves).

When the user presses arrow up: the cube should go farther from the camera, so + movement on the z axis, when arrow down is pressed: the cube should come closer to the camera so -z movement. When left is pressed, go to left i believe this is -x movement, and right, go to right which will be +x movement.

I am using a rigidbody for rotation, because I saw this used in combination with animation movement in a tutorial, later i will replace this transform translate movement for animation movement :)

Status at the moment: when I press left or right, the cube moves towards the camera -z movement and when i press up or down the cube moves from the camera +z axis movement.

UPDATE: it's the rotating script that causes the strange movement, anyone ideas about how to fix this? I want to cube to look to the direction it is moving.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     public float turnSmoothing = 15f;
     public float movementSpeed = 25f;
     
     void FixedUpdate() {
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         MovementManagement(h, v);
     }
     
     void MovementManagement(float horizontal, float vertical) {
         if (horizontal != 0f || vertical != 0f) {
           Rotating(horizontal, vertical);
           Move(horizontal, vertical);
         }
     }
     
     void Move(float horizontal, float vertical) {
         Vector3 v3 = new Vector3(horizontal, 0f, vertical);
         transform.Translate(v3, transform);
     }
     
     void Rotating(float horizontal, float vertical) {
         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
         Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
         rigidbody.MoveRotation(newRotation);
     }
     
 }
 
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 robertbu · Jul 11, 2013 at 11:35 PM 0
Share

I'm confused about your movement model. Typically movement code assigns "vertical" to move the object forward and backwards and "horizontal" to rotate the object. Sometimes the model is one of a compass where horizontal and vertical is combined to indicate a direction of movement. Your code doesn't fit into either model cleanly. Plus it is unusual to use Transform.Translate() for movement but to use Rigidbody.$$anonymous$$oveRotation() for the rotation. Please provide a description of how "Horizontal" and "Vertical" should be applied to the rotation and movement of the object.

avatar image kevinv321 · Jul 12, 2013 at 12:20 PM 0
Share

@robertbu, you're right, I'm sorry I will update the question in the hope to make myself more clear.

2 Replies

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

Answer by kevinv321 · Jul 12, 2013 at 01:36 PM

The movement with translate should be based on world instead of cube since we are rotating the cube with our code. The solution:

 Vector3 v3 = new Vector3(horizontal * movementSpeed * Time.deltaTime, 0f, vertical * movementSpeed * Time.deltaTime);
 transform.Translate(v3, Space.World);
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 ConfusedEdBoy · Aug 02, 2016 at 12:35 AM 0
Share

The movement is pretty choppy.

avatar image
0

Answer by amphoterik · Jul 12, 2013 at 11:56 AM

You are making this much more complicated than it needs to be. I am assuming that you do not want the camera to move (because you never said anything about it) and that you do not need the cube to rotate. Thus your fixed update method should just be:

 void FixedUpdate() {
     float h = Input.GetAxis("Horizontal");
     float v = Input.GetAxis("Vertical");
     transform.Translate(h * speed * Time.deltaTime, 0f, v * speed * Time.deltaTime);
 }

I have added Time.deltaTime to make the game consistent on all platforms. I have added a speed variable to easily allow you to control how fast the cube is going. Simply change the speed variable to change the cubes speed.

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 kevinv321 · Jul 12, 2013 at 01:10 PM 0
Share

@amphoterik Thanks a lot for your answer! You code indeed works I started thinking, its the same as $$anonymous$$e, so after a bit playing with my code I found out that the problem is the rotation script. That makes my cube confused, any chance you could give a solution without losing the rotation effect?

avatar image amphoterik · Jul 12, 2013 at 01:42 PM 0
Share

It looks like you got it already.

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

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

Related Questions

Need help with script and trigger, switching cameras 2 Answers

Change Player MiniMap Icon 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Load scene when colliding on GameObject 1 Answer

My Camera lookAt script doesn't work 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