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));
}
}
}
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
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);
}
}
I don't want to use Translate because it makes collisions look choppy.
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.
Directly changing the position of the object is teleporting it, which is what I am trying to avoid.