- Home /
Quaternion.Slerp is still working after hitting wall collider and doesnt stop
this code is working fine but when my character get hit by wall collider Quaternion.Slerp doesn't stop. can you help please?
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float speed = 5f;
public float rotateSpeed;
void Start ()
{
yAxis = gameObject.transform.position.y;
}
RaycastHit hit;
Ray ray;
private float yAxis;
private Vector3 endPoint;
bool moving = false;
void FixedUpdate()
{
if(Input.touchCount > 0 || Input.GetMouseButtonDown(0))
{
//ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit, 300))
{
endPoint = hit.point;
endPoint.y = yAxis;
Debug.DrawLine (ray.origin, hit.point);
}
//if (Input.GetTouch (0).phase == TouchPhase.Began)
if ( Input.GetMouseButtonDown(0))
{
moving = true;
}// else if(Input.GetTouch(0).phase == TouchPhase.Ended)
else if ( Input.GetMouseButtonUp(0))
{
moving = false;
}
}
if(moving)
{
if (!Mathf.Approximately (gameObject.transform.position.magnitude, endPoint.magnitude))
{
gameObject.transform.position = Vector3.Lerp (gameObject.transform.position, endPoint, 1 / (speed * (Vector3.Distance (gameObject.transform.position, endPoint))));
//transform.LookAt (endPoint);
var rotation = Quaternion.LookRotation(endPoint - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotateSpeed);
}
else if (Mathf.Approximately (gameObject.transform.position.magnitude, endPoint.magnitude))
{
moving = false;
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612100249im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast not hitting collider 2 Answers
Wheel Collider Rotation Problem 0 Answers
Colliders attached to objects on separate layers are colliding? 1 Answer