I made player movement script. By that you can move player with mouse/finger to left/right and I want to add code that will make player move forward on it's own on start. So basically like platform runner. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour{
private SwerveInputSystem _swerveInputSystem;
[SerializeField] private float swerveSpeed = 5f;
[SerializeField] private float maxSwerveAmount = 1f;
void Start(){
_swerveInputSystem = GetComponent<SwerveInputSystem>();
}
void Update(){
float swerveAmount = Time.deltaTime * swerveSpeed * _swerveInputSystem.MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
}
CodePudding user response:
Introduce a verticalSpeed
variable and include that in your Translate()
call.
// You can, of course, call this whatever you'd like.
[SerializeField] private float verticalSpeed;
void Update(){
float swerveAmount = Time.deltaTime * swerveSpeed * _swerveInputSystem.MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
float verticalDelta = verticalSpeed * Time.deltaTime;
// Plug verticalDelta into Translate(). Whether you use it in the y or z component depends on your game.
transform.Translate(swerveAmount, verticalDelta, 0);
}