infamous first light run code help
Hey guys! c: Im a noob programmer, ive been trying to program for years. ive only just gotten the hang of it however. took me a while to rack my brain around concepts and stuff and i lacked he focus to put in the time. Im starting to get there now.
I created this movement script from scratch. Basically. if you hold the Z key and run into a wall, you can run up the wall. if you just in the air, you jump via the transform up direction no matter what wall youre on. If youre not grounded then your rotation resets to the world rotation.
Im trying to create a run system like Infamous first light. But starting in 2D so its a little easier first.
Heres whats messng me up. I cant get the raycast to work on the lefthand side to read for a wall on the left. And it might have something to do with the rotation, but I cant figure out what it is exactly. And when i jump in the air the rotation is always facing right. Also when running along the wall, sometimes when letting go of the Z key, it doesnt do what ts supposed to do and get off the wall, and change back rotations. Sometimes it just sticks to the wall as if youre still holding it.
Thanks for the help in advance, ill keep racking my brain around it and trying to rework the code in the mean time. PS: Ignore some of the unused variables such as lineRenderer, i use it to test the length of raycast and such.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public bool grounded;
public bool facingRight;
Vector3 playerUpVector;
Vector3 originalUpVec;
Quaternion originalRot;
Vector3 surfaceNormal;
Rigidbody rb;
Renderer rend;
LineRenderer[] lineRends;
float gravity = 10;
float walkSpeed = 10;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
rend = GetComponent<Renderer>();
playerUpVector = transform.up;
originalUpVec = transform.up;
rb.freezeRotation = true;
lineRends = GetComponentsInChildren<LineRenderer>();
originalRot = transform.rotation;
facingRight = true;
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
Vector3 movement = transform.right * h * Time.deltaTime * walkSpeed;
if (movement.magnitude > 0)
{
if (h < 0)
{
facingRight = false;
transform.rotation = Quaternion.Euler(0, 180, 0);
movement = transform.right * -h * Time.deltaTime * walkSpeed;
rb.MovePosition(rb.position + movement);
}
else if (h > 0)
{
facingRight = true;
transform.rotation = Quaternion.Euler(0, 0, 0);
rb.MovePosition(rb.position + movement);
}
}
Debug.DrawRay(new Vector3(rend.bounds.extents.x,rend.bounds.center.y,0), new Vector3(rend.bounds.center.x - (rend.bounds.extents.x/2), -rend.bounds.extents.y - .4f,0),Color.red);
}
void FixedUpdate()
{
if (grounded && transform.up != Vector3.up)
{
rb.AddForce(-gravity * rb.mass * playerUpVector);
}
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(transform.up * 500 );
}
RaycastHit hit;
if (Input.GetKey(KeyCode.Z))
{
if (facingRight && Physics.Raycast(transform.position, transform.right, out hit, .501f))
{
surfaceNormal = hit.normal;
Quaternion newRot = Quaternion.LookRotation(transform.forward, surfaceNormal);
transform.rotation = newRot;
rb.useGravity = false;
playerUpVector = hit.normal;
}
if (!facingRight && Physics.Raycast(transform.position, transform.right, out hit, .51f))
{
surfaceNormal = hit.normal;
Quaternion newRot = Quaternion.LookRotation(transform.forward, surfaceNormal);
transform.rotation = newRot;
rb.useGravity = false;
playerUpVector = hit.normal;
}
}
if (Physics.Raycast(transform.position, -transform.up, out hit, .51f))
{
grounded = true;
}
else
{
grounded = false;
}
if (Input.GetKeyUp(KeyCode.Z) || !grounded)
{
rb.useGravity = true;
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y,originalRot.z);
playerUpVector = originalUpVec;
}
}
}
Answer by Nekoluffy · Dec 23, 2016 at 03:49 AM
So I figured out what to doooooo for the most part. I lacked an understanding of vectors and vector cross products. But after a little tinkering. I got it.
if (Physics.Raycast(ray, out hit, castDist))
{
//Vector3 myForward = Vector3.Cross(transform.up, hit.normal);
Quaternion dstRot = Quaternion.LookRotation(transform.forward, hit.normal);
transform.rotation = dstRot;
}
Basically you take the transforms forward and the hit.normal and you put it into quaternion.lookrotation. This gives you the rotation you want your character to face in a 2D environment.
the hit.normal picks up the wall on the right. so it works great on 90 degree angles, might wanna adjust it for cases where the surface is more rounded like a cylinder.
Your answer
Follow this Question
Related Questions
Camera don't follow sphere as was expected 0 Answers
Problem making my character walk backward 0 Answers
Trying to figure out WallRiding, with Vector3.Cross 0 Answers
Faux Gravity Character Rotate Problem 0 Answers
wall check is always returning true even though the distance doesn't fit my parameters :( 1 Answer