I want my character to move in one direction at all times (a running sidescroller game), and it still needs to use gravity.
I tried transform.Translate but that forces the character to go trough objects.
Can someone help me? I’m still quite new to Unity.
using UnityEngine;
using System.Collections;
public class scr_Player : MonoBehaviour {
public float sspeed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector2 moveDirection = Vector2.zero;
void Start () {
}
void Update () {
// transform.Translate(0.10F, 0, 0 * Time.deltaTime);
//Didn't work as intended.
//Forced the character through objects.
CharacterController player = GetComponent<CharacterController>();
if (player.isGrounded) {
moveDirection = new Vector2();
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= jumpSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
player.Move(moveDirection * Time.deltaTime);
}
}