I’m an extreme noob, literally been using Unity for a few days and learning the scripting… I created this as I thought it’d work to move left, right forward and back, and it does… but it rises into the air when I go forward lol. What kinda code would you use to stop that? Here’s what I got (Sorry if it’s badly written)
using UnityEngine;
using System.Collections;
public class playerMove : MonoBehaviour {
public float moveSpeed;
public float stopMoving;
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W)){
rigidbody.velocity = transform.forward * moveSpeed;
}
else if (Input.GetKey(KeyCode.S)){
rigidbody.velocity = transform.forward * -moveSpeed;
}
else if (Input.GetKey(KeyCode.D)){
rigidbody.velocity = transform.right * moveSpeed;
}
else if (Input.GetKey(KeyCode.A)){
rigidbody.velocity = transform.right * -moveSpeed;
}
else {
rigidbody.velocity = transform.forward * stopMoving;
}
}
}
Your code has no jump functionality in it so without knowing how you are going to handle jumping its hard to give any help.
Its can be done with a raycast to see if the object is grounded thus allowing the player to jump if it is, you can also use rays to keep the object from floating away in a similar fashion.
The problem is that when you look up, the transform.forward also looks up, so increasing the velocity in that direction will make you fly. Without knowing any more specifics, I would just advise you to use the character controller Unity has built-in, in the assets page.