- Home /
How do i keep my characters orientation?
i have a character moving in 3d space, its cool. he moces and turns and orients based on his movement. but he just wont keep that orientation of his previous movment.
if(horizontal==0 && vertical==0)targetDirection=new Vector3(0,0,1);
is the line of code I'm using to decide the players orientation. you can see it says if he isnt moving in horizontal or vertical either he should then face positiveZ.
I'm trying to change that part so he can maintain his last orientation when he stops moving. i just don't know what to put beccause i suck at scripting. ><''
please help unity forums! im at a total loss. qq
also here's the whole script:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 12f;
public float turnSmoothing = 15f;
public float speedDampTime = 0.1f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
//int floorMask;
//float camRayLength = 100f;
void Awake ()
{
//floorMask = LayerMask.GetMask ("Floor");
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
Rotating (h, v);
Animating (h, v);
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
void Rotating (float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
if(horizontal==0 && vertical==0)targetDirection=new Vector3(0,0,1);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp (rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.MoveRotation (newRotation);
}
void Animating (float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613181031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How could I add ui images to an instantiated prefab by photon ?? 0 Answers
Player move from waypoint to waypoint? 1 Answer
Play animation with FPS controller? 2 Answers
Wall Jump Scripting Help 2 Answers
Tilt object x degrees, smoothly 0 Answers