system
September 21, 2015, 9:31am
1
Hey there !
I’m working on a follow script, which is working fine, but I don’t want my following character to change it’s Y axis while going down slopes and hills, only to change the X and Z axises.
I’ve been trying to lock it through scripts like:
if (waypointTarget != null && distance > distToStop) {
float lockPos = 0.0f;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointTarget.transform.position - transform.position), rotationSpeed*Time.deltaTime);
transform.rotation = Quaternion.Euler(transform.rotation.x,lockPos,transform.rotation.z);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
but that didn’t work. How shall I change my original script to work as I intended ?
if (waypointTarget != null && distance > distToStop) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointTarget.transform.position - transform.position), rotationSpeed*Time.deltaTime);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
Thanks in advance !
Cherno
September 21, 2015, 10:29am
2
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointTarget.transform.position - transform.position), rotationSpeed*Time.deltaTime);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0f, transform.eulerAngles,z);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
system
September 24, 2015, 3:42pm
3
I finally managed to lock the axises so it only rotates by the Y axis.
Vector3 waypointOffset = waypointTarget.transform.position - new Vector3(0.0f,5.0f,0.0f);
Vector3 moveDirection = waypointOffset - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointOffset - transform.position), rotationSpeed*Time.deltaTime);
transform.rotation = new Quaternion(0.0f, transform.rotation.y, 0.0f,transform.rotation.w);
controller.Move(moveDirection.normalized * moveSpeed * Time.deltaTime);
I also changed from using transform.position to use CharacterController.move , to check collisions and to snap to terrain and to other objects like a bridge or anything.