I don’t use Character Controllers that came with Unity. Here’s the script I made for movement. The game is a top down shooter (Like The Binding of Isaac).
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float PlayerSpeed;
public float AmountToMoveH;
public float AmountToMoveV;
public int lives = 3;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(lives == 0)
Debug.Log("Death");
//Play animation
//Application.LoadLevel(death);
if(lives < 0)
lives = 0;
if(lives > 8)
lives = 8;
AmountToMoveH = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
transform.Translate(Vector3.right * AmountToMoveH);
AmountToMoveV = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;
transform.Translate(Vector3.up * AmountToMoveV);
}
void OnTriggerEnter(Collider other){
if(other.tag == "Enemy") {
Debug.Log("Collision with enemy detected");
lives -= 1;
}
}
}
I also have another problem. If and when the player shoots, he moves and rotates a little bit. Does the Rigidbody cause this?
Raycasting works with Vectors. Imagine that you have an origin, probably somewhere in the body of your character and out of that 3D vector you shoot a Ray which is a 3D Vector aswell. it has the attributes x,y,z and length. When the Ray hits the desired surface (use tags on world objects) you can make actions happen.
Example: Shoot a ray out of the characters hip downwards in a length that is equal to the point right below the characters feet. When the ray hits a surface you can say MYCharacterGO.transform.position=new Vector3(MYCharacterGO.transform.position.x , hit.y , MYCharacterGO.transform.position.z); which would set your characters y axis to the height where the surface has been hit by the ray.
This is jst a simple example and is not useable for charactermovements, as you want to make the character fall while the ray doesnt hit anything