I am having a difficult time implementing a moving script into my game(3D). Is there a built in script or something like that I can use?
use the character input asset
Not that I know of, is it a top down game first person or a third person game?
but try this (may be glitchy if it isn’t top down)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour {
public int rotationSpeed = 520;
public float walkSpeed = 5;
public float runSpeed;
private Quaternion targetRotation;
private CharacterController controller;
private Camera camera;
public Gun gun;
void Start()
{
controller = GetComponent();
camera = Camera.main;
}
void Update()
{
//ControlMouse();
ControlWASD();
if (Input.GetButtonDown(“Shoot”))
{
gun.Shoot();
}
else if (Input.GetButton(“Shoot”))
{
gun.ShootAuto();
}
}
void ControlWASD()
{
Vector3 input = new Vector3(Input.GetAxis(“Horizontal”), 0, (Input.GetAxis(“Vertical”)));
if (input != Vector3.zero)
{
targetRotation = Quaternion.LookRotation(input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
}
Vector3 motion = input;
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7f : 1;
motion *= (Input.GetButton(“Run”)) ? runSpeed : walkSpeed;
motion += Vector3.up * -8;
controller.Move(motion * Time.deltaTime);
}
void ControlMouse()
{
Vector3 mousePos = Input.mousePosition;
mousePos = camera.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, camera.transform.position.y - transform.position.y));
targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x, 0, transform.position.z));
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
Vector3 input = new Vector3(Input.GetAxisRaw(“Horizontal”), 0, (Input.GetAxisRaw(“Vertical”)));
Vector3 motion = input;
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7f : 1;
motion *= (Input.GetButton(“Run”)) ? runSpeed : walkSpeed;
motion += Vector3.up * -8;
controller.Move(motion * Time.deltaTime);
}
}
put that in a script (C#) and it should automaticaly make a CharacterController so remove a RigidBody in you player(if it has one) and try it out.
oh nvm
It is a third person game.