- Home /
problem with dead space kinesis module system
I am trying to get the kinesis module system of dead space, not the hability of hold and launch objects, but the the system that allows the user to displace an object over a rail, forward and backwards.
What I did is to get the position of the object and the player, get the substraction, and that is the velocity applied to the object. Next is to substract the local x and y velocity, so the only velocity of the object is forward or backward.
The problem is that I dont get the code works, or maybe my logic is not correct, so if anyone can help to resolve this. The code I have used is this:
Vector3 nextPos = playerCam.transform.position + playerCam.transform.forward;
Vector3 currPos = objectHeld.transform.position;
objectHeld.rigidbody.velocity = (nextPos - currPos) * 10;
Vector3 localVelocity=transform.InverseTransformDirection(objectHeld.rigidbody.velocity);
localVelocity =localVelocity -objectHeld.transform.right*objectHeld.rigidbody.velocity.x;
localVelocity =localVelocity -objectHeld.transform.up*objectHeld.rigidbody.velocity.y;
objectHeld.rigidbody.velocity =transform.TransformDirection(localVelocity);
In the picture you can see a door that only can be open or close with the kinesis system, so you cant grab it, only displace it.
EDIT: Currently, I am thinking in other way, because in this system the position of the player doesnt care, only the direction where the camera is looking, so what I need is to know how to obtain the forward of the camera respect the position of the object to displace, so if the camera looks to the forward of the object, this is displaced to its forward, or if I look to the top of the object, this moves in vertical direction, etc...
Also, to this instead of add velocity to the object, I think the best way is to use transform.tranlate, but I dont get the correct way to get this throught the code.This is what I have:
Vector3 targetDir = objectHeld.transform.position - playerCam.transform.position;
Vector3 forward = playerCam.transform.forward;
float angle = Vector3.Angle(targetDir, forward);
objectHeld.transform.Translate(Vector3.forward*Time.deltaTime);
With this I get the angle between the camera and the object, so in the translate I need a value that multiply the directorn by 1 or -1 according of the direction where the camera is looking for, behind or front of the object. Any ideas?
Answer by Visulth · Jun 15, 2015 at 03:25 AM
While I'm sure someone else will be able to give you the exact code to fit yours, until then, Unity appears to have a very similar example outlined in the manual for Vector3.Project (check the link, it has diagrams that help explain the concept):
"An example of the usage of projection is a rail-mounted gun that should slide so that it gets as close as possible to a target object. The projection of the target heading along the direction of the rail can be used to move the gun by applying a force to a rigidbody, say:"
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Slide(Transform target, Vector3 railDirection) {
Vector3 heading = target.position - transform.position;
Vector3 force = Vector3.Project(heading, railDirection);
GetComponent<Rigidbody>().AddForce(force);
}
}
Your answer
Follow this Question
Related Questions
Converting a Velocity 1 Answer
Rotation based on velocity on one axis 1 Answer
Throwing knife doesn't throw correctly 0 Answers
Rigid Body relative speed 1 Answer
How can you apply force or set the velocity of a rigidbidy on local axis? 1 Answer