Skip to main content

096. Unity3d Tutorial - Player Movement 2.0 Part 5

See video

We continue with another tutorial in our Hack And Slash demo made with the Unity3d Game Engine.

In this tutorial, we add some temporary code to stop our character from floating, and also start copying key animations that we will want to have the same names across all of out models so we can rename them and edit them if we so desire.

Tags:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Offline
Joined: Aug 7 2011

Hi!

Great Tutorials!

Keep on going the good work!

I have tried a different Character Movement Script with a Mouse Movement and Inputs, but my Character runs through uneven terrain. Maybe you can help me? Ive tried a few things by myself but everything failed.

Here is my code:


using UnityEngine;
using System.Collections;

[RequireComponent (typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour {
private Transform _myTransform;
private CharacterController _controller;
private Vector3 _targetPosition;

public int moveSpeed = 10;

public void Awake(){
_myTransform = transform;
_controller = GetComponent();

}

// Use this for initialization
void Start () {
animation.wrapMode = WrapMode.Loop;
}

// Update is called once per frame
void Update () {
if(!_controller.isGrounded){
_controller.Move(Vector3.down * Time.deltaTime);
}

CharacterMouseMovement();
}

private void CharacterMouseMovement(){
if(Input.GetMouseButton(0)){
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;

if(playerPlane.Raycast(ray, out hitdist)){
Vector3 targetPoint = ray.GetPoint(hitdist);
_targetPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - _myTransform.position);
_myTransform.rotation = targetRotation;
}

_myTransform.position = Vector3.MoveTowards(_myTransform.position, _targetPosition, Time.deltaTime * moveSpeed);
//_controller.Move(_myTransform.TransformDirection(Vector3.MoveTowards(_myTransform.position, _targetPosition, Time.deltaTime)));
animation.CrossFade("run");
}
else
animation.CrossFade("idle");
}
}

Thx 4 Help :)