As the topic explains i want to move my 3d character in between 3 lines let’s say left, mid and right. But i could not do it with neither transform.translate nor with this code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour {
private Rigidbody rb;
public float speed = 3f;
private Vector3 forwardMovement = Vector3.zero;
bool mid = true;
bool left = false;
bool right = false;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void Update ()
{
Move ();
}
private void Move()
{
Vector3 sides = new Vector3 (0, 0, 12);
forwardMovement.x = speed * Time.deltaTime;
if (Input.GetKey ("left") && mid &! left ) {
rb.MovePosition (transform.position + forwardMovement + sides * Time.deltaTime);
mid = false;
left = true;
right = false;
} else if (Input.GetKey ("right") && mid &! right) {
rb.MovePosition (transform.position + forwardMovement - sides * Time.deltaTime);
mid = false;
right = true;
left = false;
} else if (Input.GetKey ("left") && right &! mid) {
rb.MovePosition (transform.position + forwardMovement + sides * Time.deltaTime);
mid = true;
right = false;
left = false;
} else if (Input.GetKey ("right") && left &! mid) {
rb.MovePosition (transform.position + forwardMovement - sides * Time.deltaTime);
mid = true;
left = false;
right = false;
} else {
rb.MovePosition (transform.position + forwardMovement);
}
}
}
What i basically want is to movement distance of my object is to be constant to left or right positions and move it smoothly. The lines are parallel to each other btw.
FixedUpdate is better for constantly moving physics.
I mostly get your idea, but not 100% sure that I understand your 3 lines idea.
Could you describe what is happening vs. what you’d like to see happening?
I think you’ll find the code easier if you have an array of 3 positions, and your position is represented by an int (the index of the current position) 0, 1, or 2.
You input handling code would be much simpler, and then you just move towards the desired position:
public float[] positions = new float[]{-1f, 0f, 1f};
public int currentPos = 1;
public float sidewaysSpeed = 1f;
void Update() {
if (Input.GetKeyDown("left") ) currentPos--;
if (Input.GetKeyDown("right") ) currentPos++;
currentPos = Mathf.Clamp(currentPos, 0, 2);
Vector3 targetPos = transform.position;
targetPos.z = positions[currentPos];
rb.MovePosition(transform.position.MoveTowards(targetPos, Time.deltaTime * sidewaysSpeed) + forwardMovement * Time.deltaTime);
}
1 Like
Thanks for support. As soon as i can try this code i notify here. And for the question methos you asked think it like subway surfers.
Okay… I had to google that lol. So, in that game, sure it animates to move from a line to another, but you cannot ever “really” be between one of the lines. Only 3 fixed points (from the gameplay footage, I saw…) I get the idea now, at least.
Okay, so first of all there was compiler errors which caused me to modify your code a little bit here it is the final version:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour {
private Rigidbody rb;
public float speed = 3f;
private Vector3 forwardMovement = Vector3.zero;
public float[] positions = new float[]{-1f, 0f, 1f};
public int currentPos = 1;
public float sidewaysSpeed = 2f;
public float gravity = -9.8f;
public float jumpSpeed =12f;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void Update()
{
if (Input.GetKeyDown("left") ) currentPos--;
if (Input.GetKeyDown("right") ) currentPos++;
currentPos = Mathf.Clamp(currentPos, 0, 2);
Vector3 targetPos = transform.position;
targetPos.z = positions[currentPos];
forwardMovement.y = gravity * Time.deltaTime;
if (Input.GetButtonDown ("Jump"))
forwardMovement.y += jumpSpeed * Time.deltaTime;
forwardMovement.x = speed * Time.deltaTime;
float step = sidewaysSpeed * Time.deltaTime;
rb.MovePosition(Vector3.MoveTowards(transform.position, targetPos, step) + forwardMovement);
}
}
it was okay and working 'till i try to add the gravity and jump. Now it seems like this rigidbody does not seem to collide with floor, ground or some other objects i put in scene. Main reason for the collisions to be broken is probably Vector3.MoveTowards because when I eliminate this code or make it like
rb.MovePosition(transform.position + forwardMovement);
collisions work. I’m a little bit confused here. Why using Vector3.MoveTowards on a rigidbody i cant use the collisions and how can i make it work.