- Importar modelado 3d en Asstes de Unity
- Agregar Riggibody
Código Character
using UnityEngine;
using System.Collections;
public class CharacterConroller : MonoBehaviour
{
public float speed = 10.0F; //Velocidad de movimiento
public float rotationSpeed = 100.0F; //Velocidad de rotación
void Update()
{
transform.Translate(0, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
transform.Rotate(0, Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime, 0);
}
}
Código Mouse
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMouseLook : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
// Use this for initialization
void Start () {
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
}
Código Jump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour {
public float moveSpeed = 5;
public float rotateSpeed = 180;
public float jumpSpeed = 20;
public float gravity = 9.8f;
CharacterController controller;
Vector3 currentMovement;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
currentMovement = new Vector3(0, currentMovement.y, Input.GetAxis("Vertical") * moveSpeed);
currentMovement = transform.rotation * currentMovement;
if (!controller.isGrounded)
currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
controller.Move(currentMovement * Time.deltaTime);
}
}
Código Jump 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptJump : MonoBehaviour {
public float moveSpeed = 5;
public float rotateSpeed = 180;
public float jumpSpeed = 20;
public float gravity = 9.8f;
CharacterController controller;
Vector3 currentMovement;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
currentMovement = new Vector3(0, currentMovement.y, Input.GetAxis("Vertical") * moveSpeed);
currentMovement = transform.rotation * currentMovement;
if (!controller.isGrounded)
currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
else
currentMovement.y = 0;
if (controller.isGrounded && Input.GetButtonDown("Jump"))
currentMovement.y = jumpSpeed;
controller.Move(currentMovement * Time.deltaTime);
}
}
Código Jump 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Salto : MonoBehaviour {
public Rigidbody rb;
public float gravity = 9.8f;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetButtonDown("Jump"))
rb.velocity = new Vector3(0, 10, 0);
else
rb.velocity = new Vector3(0, -1, 0);
}
}
Comentarios
Publicar un comentario