Ok, I have a spider game I am working on, I was originally messing with it with using the just Horizontal and Vertical to rotate the spider etc. Well, I decided to try to incorporate a joystick, that the spider will look and move in the direction the joystick is being held down (i probably should eventually change this relevant to the camera angle, but that’s another day). Anyhow, I found a “Walk On Walls” script online, and modified it some so I can have my spider automatically start crawling on walls while I am going forward towards them. I won’t post all the code, but, I will post a lot lol. I have NO problem when using Horizontal and Vertical keys, (simply rotate left and right, Vertical makes you move forward in the Z axis)My problem is, now that I have a joystick incorporated, when I try to crawl up walls, the spider keeps using my joystick direction it was using while on the ground. It doesn’t “rotate” if you will, with the spider itself. Here is the code
///// JOYSTICK KEYS!!! ////////
var h: float = Input.GetAxis ("Horizontal");
var v: float = Input.GetAxis ("Vertical");
var vk: float = Input.GetAxis ("VerticalKeyboard");
var hk: float = Input.GetAxis ("HorizontalKeyboard");
//if (myNormal == uprightNormal)
//{
direction = new Vector3( h, 0, -v);
//}
//else if(myNormal == uprightNormalNegative)
//{
//direction = new Vector3( h, 0, v);
//}
//else
//{
//direction = new Vector3( h,-v,0);
//}
var rotation = Quaternion.LookRotation(direction, myNormal);
//Debug.Log(direction.magnitude);
if(direction.magnitude > 0.75f)
{
transform.rotation = rotation;
}
/////// KEYBOARD KEYS!!! //////////
transform.Rotate(0, Input.GetAxis("HorizontalKeyboard")*turnSpeed*Time.deltaTime, 0);
// update surface normal and isGrounded:
ray = Ray(transform.position, -myNormal); // cast ray downwards
if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
}
else {
isGrounded = false;
// assume usual ground normal to avoid "falling forever"
// Vector3.up
surfaceNormal = uprightNormal;
}
myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
// find forward direction with new myNormal:
var myForward = Vector3.Cross(transform.right, myNormal);
// align character to the new myNormal while keeping the forward direction:
var targetRot = Quaternion.LookRotation(myForward, myNormal);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
// move the character forth/back with Vertical axis:
if (canMove)
{
/////// for KEYBOARD KEYS!!! /////////
if (vk || hk != 0)
{
transform.Translate(0, 0,vk*moveSpeed*Time.deltaTime);
}
if(direction.magnitude > 0.1f)
{
/////// FOR JOYSTICK!!! //////////
transform.Translate(0, 0,moveSpeed*Time.deltaTime*direction.magnitude);
}
}
}
function HasJumpedFalse()
{
hasJumped = false;
}
function HasJumped()
{
canAttachToWall = false;
rigidbody.velocity += jumpSpeed * myNormal;
//my added code
if (myNormal != uprightNormal)
{
if (myNormal == uprightNormalNegative)
{
myNormal = uprightNormal;
transform.Rotate(Vector3(0,180,0));
}
else
{
myNormal = uprightNormal;
//transform.Rotate(Vector3(0,180,0));
}
}
}
function CanMoveFunc()
{
canMove = true;
}
function CannotMoveFunc()
{
canMove = false;
}
function JumpToWall(point: Vector3, normal: Vector3){
// jump to wall
jumping = true; // signal it's jumping to wall
rigidbody.isKinematic = true; // disable physics while jumping
var orgPos = transform.position;
var orgRot = transform.rotation;
var dstPos = point + normal * (distGround + 0.1); // will jump to 0.5 above wall
var myForward = Vector3.Cross(transform.right, normal);
var dstRot = Quaternion.LookRotation(myForward, normal);
for (var t: float = 0.0; t < 0.005f; ){
t += Time.deltaTime;
transform.position = Vector3.Lerp(orgPos, dstPos, t);
transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
yield; // return here next frame
}
myNormal = normal; // update myNormal
rigidbody.isKinematic = false; // enable physics
jumping = false; // jumping to wall finished
}
It is mostly only the beginning of this code that I need help with. Like I said, I found a javascript for a “walk on walls” script, modded it some and got what I got. As you can see, I am using a “direction” vector3 that takes into account the Joysticks Horizontal and Vertical positions. When I crawl on the wall, it is still trying to make LOOK FORWARD probably because of the “Quaternion.LookRotation” code. But, I am lost. Do you people get what I am trying to do? Lol, I am so dumbfounded right now that I probably am not making too much sense. If anyone has any ideas or suggestions, that’d be great! I’m thinking it’s gotta be something to do with using the Spider’s current transform rotation and somehow applying the controller direction vector3… i dunno -.- Thanks!