How to push player left and right

I need my player to go always forward and i want it to swing in opposite directions every time on click.
It should be like on this game but in 3D

Although this is made for Unity 2D it should not be too far a stretch to convert into 3D. Using this code we can force the player to constantly be going downwards, also allowing the player to use any key you decide on to swing them left and right. It will also check for you the rotation so that the player can not over swing, which you will have to tinker with to your liking. Some of the code will certainly have to be edited to fit you’re liking, as well as turn it into 3D. Hopefully it will be sufficient. Where it says transform.position -= transform.up * Time.deltaTime * speed;

That is where you change transform.up to transform.forward to work in unity 3D.


using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour
{
	public Rigidbody2D playerOne;
	public int speed;
	public KeyCode leftKey;
	public KeyCode rightKey;
	public Vector2 towardsPoint;
	public int rotateSpeed;

	// Use this for initialization
	void Start ()
	{
		playerOne.velocity = towardsPoint * 10;

	}

	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKey (leftKey) && playerOne.transform.rotation.eulerAngles.z <= 90) {

			transform.Rotate (0, 0, rotateSpeed * Time.deltaTime, Space.World);
			transform.position -= transform.up * Time.deltaTime * speed;

		}
		if (Input.GetKey (rightKey) && playerOne.transform.rotation.eulerAngles.z <= 90) {

			transform.Rotate (0, 0, -rotateSpeed *Time.deltaTime, Space.World);
			transform.position -= transform.up * Time.deltaTime * speed;

		}
	}
}

  • Other Unity User’s Questions that Helped Figure This One Out

Move in Direction Facing

Don’t Overturn

Constant Speed

Constant Speed 2
https://gamedev.stackexchange.com/questions/101025/moving-a-character-depending-on-the-direction-he-is-facing-c-unity3d


Hopefully this has at least helped a little and someone will come along and make this even easier as I am a novice to 3D and 2D Unity.