Question by
Kun-No-Name · Jun 10, 2016 at 12:09 PM ·
rotationcolliderhumanoid
Need help with walk on the wall and ceiling
I want to create the game that control with keyboard like key arrow and the character can walk to the wall smoothly. I think i will use the local gravity to be perpendicular and use collider to curve between floor and wall then when youwalk the rotation will change by your rotation and make your feet stick to the floor while change gravity graually. This is my code. using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour
{
float speed = 2f;
Vector3 movement;
Vector3 currentVector;
Rigidbody playerRB;
Animator anim;
Quaternion targetRotation;
public float RotationSpeed = 400f;
public Transform GravityPoint;
Ray downRay;
void Awake ()
{
anim = GetComponent<Animator>();
playerRB = GetComponent<Rigidbody>();
downRay = new Ray(transform.position, (GravityPoint.position - transform.position).normalized);
//currentVector = (GravityPoint.position - transform.position).normalized;
}
void Start()
{
}
void Update()
{
if(Physics.Raycast(downRay, 100))
{
}
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
Rotate(h,v);
Animate(h, v);
currentVector = (GravityPoint.position - transform.position).normalized;
playerRB.AddForce(currentVector * 10);
Debug.Log(transform.eulerAngles.x );
Debug.Log(transform.eulerAngles.y);
Debug.Log(transform.eulerAngles.z);
}
void Move(float h ,float v)
{
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRB.MovePosition(transform.position + movement);
}
void Rotate(float h , float v)
{
if(h == 0 && v == 1)
{
targetRotation = Quaternion.AngleAxis(315f , Vector3.up);
}
else if(h == 1 && v == 1)
{
targetRotation = Quaternion.AngleAxis(0f, Vector3.up);
}
else if(h == 1 && v == 0)
{
targetRotation = Quaternion.AngleAxis(45f, Vector3.up);
}
else if (h == 1 && v ==-1)
{
targetRotation = Quaternion.AngleAxis(90f, Vector3.up);
}
else if (h == 0 && v == -1)
{
targetRotation = Quaternion.AngleAxis(135f, Vector3.up);
}
else if (h == -1 && v ==-1)
{
targetRotation = Quaternion.AngleAxis(180f, Vector3.up);
}
else if (h == -1 && v == 0)
{
targetRotation = Quaternion.AngleAxis(225f, Vector3.up);
}
else if (h == -1 && v == 1)
{
targetRotation = Quaternion.AngleAxis(270f, Vector3.up);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, RotationSpeed * Time.deltaTime);
}
void Animate(float h, float v)
{
bool walking = (h != 0f) || (v != 0f);
anim.SetBool("Iswalking", walking);
}
/*void OnDrawGizmos()
{
Gizmos.DrawRay(ray);
}*/
}
Now the problem is when my character is invert and i hit the button to move it will try to make rotation back to normal like (0,0,0) and I don't know how to solve this. If you have any suggestion please tell me. Thank you very much.
curve.png
(480.9 kB)
Comment