Maintaining the Difference Between 2 Objects
I'm attempting to roll a standard sphere using addforce while floating my character model at the same x,y and z coords that I have in the picture. However, when I go to roll the ball, the character floats up on the y axis to some consistent point and stays there, instead of sitting directly on the ball. Also, the character has no colliders(yet, and they will be from the torso up)
This is my following code:
using UnityEngine;
using System.Collections;
public class Following : MonoBehaviour
{
// The target we are following
public Transform target;
public int distance = 0;
public float height = 1;
public float heightDamping = 2.0f;
public float rotationDamping = 0.6f;
//public float rayLength;
//RaycastHit rayHit;
//Ray ray;
/*void FixedUpdate()
{
ray.origin = transform.position;
ray.direction = Vector3.down;
if (Physics.Raycast(ray, out rayHit, 8))
{
rayLength = transform.position.y - rayHit.transform.position.y;
if (rayLength > 0.2f)
transform.Translate(transform.position +
new Vector3(transform.position.x, -rayLength + 0.1f, transform.position.z));
}
}*/
void LateUpdate()
{
if (!target)
return;
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
}
}
Your answer
Follow this Question
Related Questions
3DS Max fbx model losing UV maps when splitting up meshes 1 Answer
How do I solve the mesh issue after exporting from 3ds max. 1 Answer
Unity problems with .FBX models,Unity3D problems with FBX model 0 Answers
Inverted Normals on Animated(Morph)Character Face 3 Answers
Exporting animation from 3DS Max 2019 doesn't seem to be working. 0 Answers