Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 $$anonymous$$ · Dec 27, 2017 at 02:53 AM · addforcecubeboxcolliderslide

How do I slide a cube forwards?

Apparently, using transform.Translate actually teleports the object rather than moving it, so I've been trying to use AddForce to move a cube around, but it seems to be flipping itself forwards rather than sliding forwards. Any help would be much appreciated.

EDIT: Here is the code for how I am currently attempting to slide the cube:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerBoxScript : MonoBehaviour {
 
     public Rigidbody TheRigidBody;
 
     public float Speed;
 
     public float ForceSpot;
 
     public string ForwardButton;
     public string BackwardsButton;
     public string TurnRightButton;
     public string TurnLeftButton;
 
     void Start () {
         
     }
 
     void Update () {
         if (Input.GetKey(ForwardButton)){
             TheRigidBody.AddForceAtPosition (Vector3.forward * Speed, gameObject.transform.position + new Vector3(0,ForceSpot,0));
         }
         if (Input.GetKey (BackwardsButton)) {
             TheRigidBody.AddForceAtPosition (Vector3.back * Speed, gameObject.transform.position + new Vector3(0,ForceSpot,0));
         }
         if (Input.GetKey (TurnRightButton)) {
             gameObject.transform.Rotate (new Vector3 (0, 1, 0));
         }
         if (Input.GetKey (TurnLeftButton)) {
             gameObject.transform.Rotate (new Vector3 (0, -1, 0));
         }
     }
 }
 
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

4 Replies

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

Answer by UDN_9a915d40-27e1-405b-b1cc-83be8be3e71d · Dec 27, 2017 at 09:26 AM

Use the velocity variable in the Rigidbody. Ex: GetComponent<RigidBody>().velocity = transform.forward * moveSpeed

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 $$anonymous$$ · Dec 27, 2017 at 07:21 PM 0
Share

This works, but only if I freeze all rotation.

avatar image
0

Answer by uk007singh · Dec 27, 2017 at 05:02 AM

 using UnityEngine;
 using System.Collections;
 
 public class MoveCube: MonoBehaviour
 {
     public float moveSpeed = 20f;
     public float turnSpeed = 40f;
     
     
     void Update ()
     {
         if(Input.GetKey(KeyCode.UpArrow))
             transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
         
         if(Input.GetKey(KeyCode.DownArrow))
             transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
         
         if(Input.GetKey(KeyCode.LeftArrow))
             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
         
         if(Input.GetKey(KeyCode.RightArrow))
             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
     }
 }
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 $$anonymous$$ · Dec 27, 2017 at 05:09 AM 0
Share

I don't want to use Translate because it makes collisions look choppy.

avatar image
0

Answer by SovietAlex · Dec 27, 2017 at 01:20 PM

Okay there are many ways to do this.

1~ Add Rigidbody to your cube, In Rigidbody go to "Constraints", and at Freeze Rotation select "x" and "z".

This way you can use AddForce method.


2~ Use transform.position = transform.position + transform.forward * distance;

distance is an integer or a float;

Or

Use transform.position = transform.position + transform.forward * distance * Time.deltaTime;

this makes the movement smoother


3~ Use transform.position = new Vector3(transform.position.x + xAmount, transform.position.y, transform.position.z + zAmount);

this allows you to

go x units to the left or right

or

go z units forward or backward

Or

Use transform.position = new Vector3(transform.position.x + 10 * Time.deltaTime, transform.position.y, transform.position.z + 10 * Time.deltaTime);

Same thing just makes it smoother.

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 $$anonymous$$ · Dec 27, 2017 at 07:18 PM 0
Share

Directly changing the position of the object is teleporting it, which is what I am trying to avoid.

avatar image
0

Answer by Lumineux · Dec 27, 2017 at 09:00 AM

You can freeze the rotation:

alt text


untitled.png (10.2 kB)
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 $$anonymous$$ · Dec 27, 2017 at 07:18 PM 0
Share

If I freeze the rotation, it does not move at all.

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

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

Related Questions

Is it better to use the Plane Mesh Collider or a Box Collider for Walls and Floors. 1 Answer

Object do not stop after collision 0 Answers

Box Coillider changes to trigger depending on speed of the object. 0 Answers

run under a box collider fix??,won't stay croched under a box collider 0 Answers

Unable to have door Slide up after pressing Ctrl to move down, Unable to find issue in my code. 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