Question by
leonxgrant · Aug 07, 2016 at 01:34 PM ·
c#movementrigidbodytransform.translate
Rigidbody movement instead of transform?
I've written some code and everything works fine -- the player looks towards the mouse, and when W/S are pressed, the player moves forwards/backwards from wherever the mouse is, which is exactly what I want for my project.
However, the only issue is, I need to move the player using Rigidbody instead of Transform.translate (for various other reasons). Can anyone translate this code into Rigidbody movement? Thanks in advance.
using UnityEngine;
using System.Collections;
public class PlayerLook : MonoBehaviour
{
public Plane playerPlane;
public Transform Player;
public Ray ray;
public AnimationClip playerIDLE;
public AnimationClip playerRUN;
public static float playerSpeed = 90;
public static bool attacking;
// Update is called once per frame
void Update ()
{
playerPlane = new Plane(Vector3.up, transform.position);
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist;
if (playerPlane.Raycast(ray, out hitdist) && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * Time.deltaTime * playerSpeed);
animation.Play(playerRUN.name);
}
else
{
animation.Play(playerIDLE.name);
}
}
}
Comment